ICSharpCode.Decompiler + Mono.Cecil能做到 - >如何生成一个单一的方法代码? [英] ICSharpCode.Decompiler + Mono.Cecil -> How to generate code for a single method?

查看:882
本文介绍了ICSharpCode.Decompiler + Mono.Cecil能做到 - >如何生成一个单一的方法代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能使用Mono.Cecil能做到和ICSharpCode.Decompiler产生的类型或汇编的代码。

I'm able to use Mono.Cecil and ICSharpCode.Decompiler to generate the code for a type or an assembly.

但是,如果我试图生成代码对于一个方法,我会得到一个错误不设置到对象的实例对象引用。

But if I try to generate the code for a single method I'll get an error "Object reference not set to an instance of an object."

你们可以给我这个任何提示?感谢前面所有帮助

Can you guys give me any hints about this? Thanks ahead for all the help.

代码生成代码的组件内的所有类型:

Code to generate code for all the types inside an assembly:

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();
        }
    }
}

代码来生成所有代码一个组件内的公共方法:

Code to generate code for all the public methods inside an assembly:

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();
                }
            }
        }
    }
}


推荐答案

我有同样的问题。您应该设置DecompilerContext的财产CurrentType。更改您的代码

I had the same problem. You should set the property CurrentType of the DecompilerContext. Change your code to

astBuilder = new AstBuilder(new ICSharpCode.Decompiler.DecompilerContext(assemblyDefinition.MainModule) { CurrentType = typeInAssembly } );

这篇关于ICSharpCode.Decompiler + Mono.Cecil能做到 - >如何生成一个单一的方法代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆