4

Mono.Cecil と ICSharpCode.Decompiler を使用して、型またはアセンブリのコードを生成できます。

しかし、単一のメソッドのコードを生成しようとすると、「オブジェクト参照がオブジェクトのインスタンスに設定されていません」というエラーが表示されます。

これについて何かヒントを教えていただけますか?どうぞよろしくお願いいたします。

アセンブリ内のすべての型のコードを生成するコード:

DirectoryInfo di = new DirectoryInfo(appPath);
FileInfo[] allAssemblies = di.GetFiles("*.dll");
foreach (var assemblyFile in allAssemblies)
{
    string pathToAssembly = assemblyFile.FullName;
    System.Reflection.Assembly assembly = System.Reflection.Assembly.ReflectionOnlyLoadFrom(pathToAssembly);
    Mono.Cecil.AssemblyDefinition assemblyDefinition = Mono.Cecil.AssemblyDefinition.ReadAssembly(pathToAssembly,parameters);
    AstBuilder astBuilder = null;

    foreach (var typeInAssembly in assemblyDefinition.MainModule.Types)
    {
        if (typeInAssembly.IsPublic)
        {
             Console.WriteLine("T:{0}", typeInAssembly.Name);
            //just reset the builder to include only code for a single type
            astBuilder = new AstBuilder(new ICSharpCode.Decompiler.DecompilerContext(assemblyDefinition.MainModule));
            astBuilder.AddType(typeInAssembly);
            StringWriter output = new StringWriter();
            astBuilder.GenerateCode(new PlainTextOutput(output));
            string result = output.ToString();
            output.Dispose();
        }
    }
}

アセンブリ内のすべてのパブリック メソッドのコードを生成するコード:

DirectoryInfo di = new DirectoryInfo(appPath);
FileInfo[] allAssemblies = di.GetFiles("*.dll");
foreach (var assemblyFile in allAssemblies)
{
    string pathToAssembly = assemblyFile.FullName;
    System.Reflection.Assembly assembly = System.Reflection.Assembly.ReflectionOnlyLoadFrom(pathToAssembly);
    Mono.Cecil.AssemblyDefinition assemblyDefinition = Mono.Cecil.AssemblyDefinition.ReadAssembly(pathToAssembly,parameters);
    AstBuilder astBuilder = null;

    foreach (var typeInAssembly in assemblyDefinition.MainModule.Types)
    {
        if (typeInAssembly.IsPublic)
        {
            Console.WriteLine("T:{0}", typeInAssembly.Name);
            foreach (var method in typeInAssembly.Methods)
            {
                //just reset the builder to include only code for a single method
                astBuilder = new AstBuilder(new ICSharpCode.Decompiler.DecompilerContext(assemblyDefinition.MainModule));
                astBuilder.AddMethod(method);
                if (method.IsPublic && !method.IsGetter && !method.IsSetter && !method.IsConstructor)
                {
                    Console.WriteLine("M:{0}", method.Name);
                    StringWriter output = new StringWriter();
                    astBuilder.GenerateCode(new PlainTextOutput(output));
                    string result = output.ToString();
                    output.Dispose();
                }
            }
        }
    }
}
4

2 に答える 2

6

私も同じ問題を抱えていました。DecompilerContext のプロパティ CurrentType を設定する必要があります。コードを次のように変更します

astBuilder = new AstBuilder(new ICSharpCode.Decompiler.DecompilerContext(assemblyDefinition.MainModule) { CurrentType = typeInAssembly } );
于 2012-11-22T08:26:32.150 に答える
3

最近、クイック C# デコンパイラ (MonoDecompiler ベース) を実装したとき、ILSpy メソッドを使用しました :)

public string getSourceCode(MethodDefinition methodDefinition)
{
    try
    {
        var csharpLanguage = new CSharpLanguage();
        var textOutput = new PlainTextOutput();
        var decompilationOptions = new DecompilationOptions();
        decompilationOptions.FullDecompilation = true;
        csharpLanguage.DecompileMethod(methodDefinition, textOutput, decompilationOptions);
        return textOutput.ToString();              

    }
    catch (Exception exception)
    {
        PublicDI.log.error("in getSourceCode: {0}", new object[] { exception.Message });
        return ("Error in creating source code from IL: " + exception.Message);
    }
}

この例と他の例については、 https ://github.com/o2platform/O2.Platform.Scripts/blob/master/3rdParty/MonoCecil/CecilDecompiler/CecilDecompiler.cs を参照してください。

スタンドアロンのミニ C# 逆コンパイル ツールは、このスクリプトによって作成されますhttps://github.com/o2platform/O2.Platform.Scripts/blob/master/3rdParty/MonoCecil/Utils/Tool%20-%20C%23%20Quick% 20Decompiler.h2

于 2012-12-19T07:49:41.487 に答える