当我在运行时获得il并且当我使用roslyn获得il时,为什么我的方法不同?

时间:2016-04-04 13:39:03

标签: c# roslyn

我有两次使用一种方法,但我不知道为什么它们不相同? 我在我的c#app中使用了il方法来加密/解密字符串。因为它们不相同,加密的字符串不会解密。

internal class ILSaver : CSharpSyntaxRewriter
{
    public Assembly asembly { get; private set; }
    public string NameSpace { get; private set; }
    public string ClassName { get; private set; }
    internal void Save(Solution solution)
    {
        foreach (Project projext in solution.Projects)
        {
            asembly = solution.GerdehamayeeRaBedeh(projext.Id);//extension method that return Assembly of project
            if (projext.Name == "WinformTest")
            {
                List<Document> ListeSanad = projext.Documents.ToList();
                for (int j = 0; j < ListeSanad.Count; j++)
                {
                    Document sanad = ListeSanad[j];
                    if (sanad.Name == "Form1.cs")
                    {
                        SyntaxNode Risheh = solution.GetDocument(sanad.Id).GetSyntaxRootAsync().Result;
                        CompilationUnitSyntax VahedeGerdAvari = Visit(Risheh) as CompilationUnitSyntax;
                    }
                }
            }
        }
    }
    public override SyntaxNode VisitNamespaceDeclaration(NamespaceDeclarationSyntax node)
    {
        NameSpace = node.Name.ToString();
        return base.VisitNamespaceDeclaration(node);
    }
    public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
    {
        ClassName = node.Identifier.ValueText;
        return base.VisitClassDeclaration(node);
    }

    public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax tabeh)
    {
        Type type = asembly.GetType(NameSpace + "." + ClassName);
        byte[] il = type.GetIL(tabeh.Identifier.ValueText);//extension Method that return il

        string IL = "";
        foreach (var item in il)
        {
            IL += item.ToString() + ",";
        }
        File.WriteAllText("ILWithRoslyn.txt", IL);

        return tabeh;
    }
}

这是我的扩展方法,它将返回方法的il。可能我在这个方法中错误地获取il。

public static byte[] GetIL(this Type InputType, string NameOfMethod)
    {
        var rms = InputType.GetRuntimeMethods();
        MethodInfo mi = (from m in rms where m.Name == NameOfMethod select m).SingleOrDefault();
        MethodBody mb = mi.GetMethodBody();
        byte[] il = mb.GetILAsByteArray();
        return il;
    }

我的扩展方法将返回项目的程序集:

 public static Assembly GerdehamayeeRaBedeh(this Solution rahkar, ProjectId id)
    {
        Project prozhe = rahkar.GetProject(id);
        Compilation Gerdavari = prozhe.GetCompilationAsync().Result;
        Assembly Gerdehamayee = null;
        using (MemoryStream ms = new MemoryStream())
        {
            Gerdavari.Emit(ms);
            Gerdehamayee = Assembly.Load(ms.ToArray());
        }
        return Gerdehamayee;
    }

这是我的winform测试应用程序:

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {
        byte[] ILOFMethod = MethodBase.GetCurrentMethod().GetMethodBody().GetILAsByteArray();
        string IL = "";
        foreach (var item in ILOFMethod)
        {
            IL += item.ToString() + ",";
        }
        File.WriteAllText("IL.txt", IL);
    }
}

0 个答案:

没有答案