.net 如何使用dnlib库获取所有方法的指令?
我正在尝试使用项目中的库来加载程序集,并获取包含在所有方法的“body”上的IL指令 我用这个VB.NET源代码编译了一个assmebly:.net 如何使用dnlib库获取所有方法的指令?,.net,vb.net,winforms,reflection,dnlib,.net,Vb.net,Winforms,Reflection,Dnlib,我正在尝试使用项目中的库来加载程序集,并获取包含在所有方法的“body”上的IL指令 我用这个VB.NET源代码编译了一个assmebly: Public Class Main Public Sub testmethod(ByVal testparameter As String) MsgBox(testparameter) End Sub Public Class Test2 Public Function testfunction
Public Class Main
Public Sub testmethod(ByVal testparameter As String)
MsgBox(testparameter)
End Sub
Public Class Test2
Public Function testfunction(ByVal testparameter As String) As String
Return testparameter
End Function
End Class
End Class
我知道编译器修改了很多东西,但我认为方法名(在本例中)不会被修改,如果我错了,请纠正我
然后我尝试用以下代码检索这些方法:
Imports dnlib.DotNet
Imports dnlib.DotNet.Emit
Private Sub Test_Handler() Handles MyBase.Shown
Dim asmResolver As New AssemblyResolver()
Dim modCtx As New ModuleContext(asmResolver)
' All resolved assemblies will also get this same modCtx
asmResolver.DefaultModuleContext = modCtx
' Enable the TypeDef cache for all assemblies that are loaded
' by the assembly resolver. Only enable it if all auto-loaded
' assemblies are read-only.
asmResolver.EnableTypeDefCache = True
Dim Assembly As ModuleDefMD = ModuleDefMD.Load("C:\WindowsApplication.exe")
Assembly.Context = modCtx
' Use the previously created (and shared) context
Assembly.Context.AssemblyResolver.AddToCache(Assembly)
Dim Members As IEnumerable(Of MemberRef) = Assembly.GetMemberRefs
For Each m As MemberRef In Members
If m.IsMethodRef Then
Dim Method As MethodDef = m.ResolveMethod
If Method.HasBody Then
Dim sb As New System.Text.StringBuilder
With sb
.AppendLine(String.Format("Method Name: {0}", Method.FullName))
.AppendLine()
.AppendLine(String.Format("Method Signature: {0}", Method.Signature.ToString))
.AppendLine()
.AppendLine(String.Format("Method Instructions: {0}", Environment.NewLine &
String.Join(Environment.NewLine, Method.Body.Instructions)))
End With
MessageBox.Show(sb.ToString)
End If
End If
Next
End Sub
问题是,我看到的这个库的唯一文档是XML文档文件和dnlib站点上帮助我编写上述代码的一些非常基本的示例,但我不确定如何解析/检索这些方法,因为我做得不正确,上面的代码无法解析我编译的任何方法(testmethod和testfunction),它向我展示了许多构造函数(.ctor)和其他方法
我想做的只是对所有方法(私有、公共等)执行一次迭代包含在我编译的源代码中,不管项目有多少类,也不管哪个类包含特定的方法,都可以得到它的指令。看起来你只是在程序集中到处乱翻,所以你得到的只是
AssemblyInfo.vb
中定义的东西。你可能想做什么是迭代程序集中的类型,然后钻取为要查找的类型定义的成员或属性
这段代码应该让您指向正确的方向:
Dim modDef As ModuleDefMD=ModuleDefMD.Load(“C:\Temp\ConsoleApplication1.exe”)
对于modDef.GetTypes中的每个t作为TypeDef
'打印类型名称
Console.WriteLine(t.Name)
'匹配类型的愚蠢方法,但用于演示目的
如果t.FullName.StartsWith(“ConsoleApplication1.Module1”),则
对于t.方法中作为MethodDef的每种方法
'打印方法名称
WriteLine(“方法:{0}”,meth.Name)
下一个
如果结束
下一个
输出如下所示,请注意,您的方法已列出
<Module>
MyApplication
MyComputer
MyProject
MyWebServices
ThreadSafeObjectProvider`1
InternalXmlHelper
RemoveNamespaceAttributesClosure
Module1
Method: Main
Method: testmethod
Test2
Method: .ctor
Method: testfunction
Resources
MySettings
MySettingsProperty
我的申请
我的电脑
我的项目
MyWebServices
ThreadSafeObjectProvider`1
内部XmlHelper
RemoveNamespaceAttributesClosure
模块1
方法:主
方法:testmethod
测试2
方法:
方法:testfunction
资源
我的设置
MySettingsProperty
该代码不适用于我提供的WinForms源代码(不是consoleapp):对于modDef中的每个t As TypeDef.GetTypes对于t.Methods中的每个meth As MethodDef如果t.Name.Contains(“test”)则Debug.WriteLine(“方法:{0}”,meth.Name)End If Next
oops,对不起,我没有注意到我试图比较类型名(t.name.contains)而不是方法名(meth.name.contains),它工作得很好