C# 为什么当我在运行时得到il和当我和roslyn得到il时,方法的il是不同的?
我两次得到一种方法的il,但我不知道为什么它们不一样? 在我的c#应用程序中,我使用了多种方法对字符串进行加密/解密。因为它们不相同,加密的字符串不会解密C# 为什么当我在运行时得到il和当我和roslyn得到il时,方法的il是不同的?,c#,roslyn,C#,Roslyn,我两次得到一种方法的il,但我不知道为什么它们不一样? 在我的c#应用程序中,我使用了多种方法对字符串进行加密/解密。因为它们不相同,加密的字符串不会解密 internal class ILSaver : CSharpSyntaxRewriter { public Assembly asembly { get; private set; } public string NameSpace { get; private set; } public string ClassN
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;
}
}
将返回项目程序集的我的扩展方法:
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);
}
}
您是否比较了不同配置(例如调试和发布)的IL?在发布模式下编译时,默认情况是优化代码,这将产生不同的IL到调试模式,默认情况下未优化。我在winform测试应用程序中使用了调试,但对于roslyn,我不知道我将更新问题以获取汇编为什么它们是相同的?您不应该依赖于IL的组装方式。你想在这里做什么?顺便说一句,
namespacedclarationsyntax.Name
不是完整的名称空间。您应该使用语义模型。正如他们在评论中提到的,您不能完全依赖于IL是如何组装的。即使你能让它工作,你将如何保护他们的钥匙?如果攻击者可以反编译您的源代码,是什么阻止他们反编译加密密钥?
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);
}
}