说明:如何使用dnlib库获取所有方法的说明-



我试图使用dnlib库,这是de4dot项目的一部分,以加载程序集,并获得IL指令包含在所有方法的"主体"上。

我用这个VB编译了一个程序集。来源:
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上面帮助我写代码的网站,但我不确定如何解决/检索这些方法'因为我不doinf得当,上面的代码不解决任何方法,我总结了( testmethod testfunction ),相反,它向我展示了大量的构造函数(.ctor)和其他方法。

我想做的就是对我编译的源代码中包含的所有方法(私有的,公共的等)进行迭代,而不管项目拥有的类的数量,也不管什么类包含特定的方法,以获得其指令。

看起来你只是在大会上闲逛,所以你得到的只是在AssemblyInfo.vb中定义的东西。您可能想要做的是迭代程序集中的类型,然后钻取为您正在查找的类型定义的成员或属性。

这段代码应该会指引你正确的方向:

Dim modDef As ModuleDefMD = ModuleDefMD.Load("C:TempConsoleApplication1.exe")
For Each t As TypeDef In modDef.GetTypes
    'print the Type name
    Console.WriteLine(t.Name)
    ' stupid way to match a Type, but will work for demo purposes
    If t.FullName.StartsWith("ConsoleApplication1.Module1") Then
        For Each meth As MethodDef In t.Methods
            ' print the method name
            Console.WriteLine("     Method: {0}", meth.Name)
        Next
    End If
Next

输出如下所示,请注意列出了您的方法。

<Module>
MyApplication
MyComputer
MyProject
MyWebServices
ThreadSafeObjectProvider`1
InternalXmlHelper
RemoveNamespaceAttributesClosure
Module1
     Method: Main
     Method: testmethod
Test2
     Method: .ctor
     Method: testfunction
Resources
MySettings
MySettingsProperty

相关内容

  • 没有找到相关文章

最新更新