Search code examples
c#decompilingmono.cecilxml-documentation

Why can't I see my XML documentation when decompiling using ICSharpCode.Decompiler?


I've been looking into decompiling .dlls using ICSharpCode.Decompiler and found some sample code and fingers in the right direction on this thread:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/00e59445-9f85-4ec5-a04f-9796a72a00a2/library-to-decompile-assembly-to-c

I've copied the code and added set the variable ShowXmlDocumentation = true in the DecompilerSettings, however I'm still unable to see my documentation.

My source code looks like this:

var settings = new DecompilerSettings
{
    FullyQualifyAmbiguousTypeNames = true,
    ShowXmlDocumentation = true
};

const string assemblyName = "Experiments.Decompilation.dll";
var assembly1 = AssemblyDefinition.ReadAssembly(assemblyName);
var decompilerContext = new DecompilerContext(assembly1.MainModule) {Settings = settings};
var decompiler = new AstBuilder(decompilerContext);

decompiler.AddAssembly(assembly1);

var output = new StringWriter();
decompiler.GenerateCode(new PlainTextOutput(output));

var byteArray = Encoding.ASCII.GetBytes(output.ToString());
TextReader codeReader = new StreamReader(new MemoryStream(byteArray));
var line = codeReader.ReadToEnd();

Yet the variable line never has any of the expected XML documentation in it.

I get the full trace of what's in the .dll, for example

namespace Experiments.Decompilation
{
    public interface ITest
    {
        long Method();
    }
}

But I was expecting the interface method to have the XML docs I defined, a la

namespace Experiments.Decompilation
{
    ///<summary>
    ///This is some test documentation
    ///</summary>
    public interface ITest
    {
        long Method();
    }
}

But no cookie for me.

Am I missing anything? Do I need to change any other configuration?

If anyone has any ideas on this I'd really appreciate it. I've been wracking my brains and haven't found a solution myself, so here you are SO, please help!


Solution

  • Because that property doesn't actually do anything within the ICSharpCode.Decompiler library. It's just there to support projects (like ILSpy) that want to consume the decompiler.

    ILSpy, for example, will check to see if the decompiler has the option set; if so, it will look up the appropriate XML file on-disk and parse the XMLDoc strings, and embed them in the final output.

    Also, note that the actual .NET assembly doesn't have the XMLDoc in it. Visual Studio generates a separate file with that stuff, and if you don't have that, ILSpy won't be able to include XMLDoc even if you ask.