我读过关于延迟初始化的文章,但实际上我不明白如何初始化里面的对象。
这个函数返回一个Lazy类型LazyList
的自定义实现
Public Function GetMethods(ByVal Assembly As String,
ByVal TypeName As String) 'As List(Of MethodDef) ' As MethodDef()
Dim methods As LazyList(Of MethodDef) = Nothing
Using ass As ModuleDefMD = ModuleDefMD.Load(Assembly)
For Each t As TypeDef In ass.GetTypes
If t.HasMethods AndAlso t.Name.String.Equals(TypeName, StringComparison.OrdinalIgnoreCase) Then
methods = t.Methods
' MsgBox(t.Methods.GetType.Name) ' Result: LazyList'1
Exit For
End If
Next t
Return methods
End Using
End Function
当我尝试读取 LazyList 的任何项目的属性时,我得到一个NullReferenceException
异常,我想这是因为对象没有初始化?因为请注意,lazylist 项目计数为“2”,我完全确定我尝试读取的属性不能为空。
Imports dnlib.DotNet
Imports dnlib.DotNet.Emit
Imports dnlib.Utils
Dim methods As LazyList(Of MethodDef) = GetMethods("C:\WindowsApplication.exe", "Main")
MsgBox(methods.IsInitialized(0)) ' Result: False
MsgBox(methods.Count) ' Result: 2
For Each method As MethodDef In methods
' NullReferenceException exception here:
MsgBox(method.Name)
' NullReferenceException exception here too (reading m.hasbody):
If method.HasBody Then
Dim sb As New System.Text.StringBuilder
With sb
.AppendLine(String.Format("Method Instructions: {0}", Environment.NewLine &
String.Join(Environment.NewLine, method.Body.Instructions)))
End With
Debug.WriteLine(sb.ToString)
End If
Next method
如果我尝试重现我在上面的代码中所做的事情(尝试读取属性)但在函数内部,那么一切都按预期进行,没有任何 nullreference 异常......就像在这个例子中:
public function GetMethods (...)
' deleted code...
If t.HasMethods AndAlso t.Name.String.Equals(TypeName, StringComparison.OrdinalIgnoreCase) Then
methods = t.Methods
For Each m In methods
MsgBox(m.Name) ' no exceptions
MsgBox(m.HasBody) ' no exceptions
Next
End If
' deleted code...
end function
PS:导入来自dnlib库。
更新:
我想用更多更好的例子来解释这个问题。
在继续解释之前有两件事:
我确保在所有示例中都TypeName
存在参数并且找到了 iis,我确保函数返回的对象集合永远不会为空,它的 Collection.Count 为2,正如我在问题开头所解释的那样,所以这都是问题所在。
好吧,下一个函数返回一个对象列表,这些对象带有一个名为的属性,HasBody
但该属性总是为空(抛出NullReference异常),当它不应该为空时,集合中包含的两个项目的值都应该是True .
Public Function GetMethods(ByVal Assembly As String,
ByVal TypeName As String) As List(Of MethodDef)
Dim methods As List(Of MethodDef) = Nothing
Using ass As ModuleDefMD = ModuleDefMD.Load(Assembly)
For Each t As TypeDef In ass.GetTypes
If t.HasMethods AndAlso t.Name.String.Equals(TypeName, StringComparison.OrdinalIgnoreCase) Then
methods = t.Methods.ToList
Exit For
End If
Next t
Return methods
End Using
End Function
另一方面,如果我在函数中执行了一个荒谬的修改(请参阅与tmp
对象相关的更改),该函数返回一个对象列表,其中HasBody
属性已初始化,不为空,并且返回列表中包含的 2 项HasBody
值为True的属性。
Public Function GetMethods(ByVal Assembly As String,
ByVal TypeName As String) As List(Of MethodDef)
Dim methods As List(Of MethodDef) = Nothing
Dim tmp As Object
Using ass As ModuleDefMD = ModuleDefMD.Load(Assembly)
For Each t As TypeDef In ass.GetTypes
If t.HasMethods AndAlso t.Name.String.Equals(TypeName, StringComparison.OrdinalIgnoreCase) Then
methods = t.Methods.ToList
For Each m In methods
tmp = m.HasBody
Next
Exit For
End If
Next t
Return methods
End Using
End Function
那么问题出在哪里以及如何解决?,也许这是这种情况的临时解决方案,但methoddef
返回函数的对象包含很多属性,我需要访问比HasBody
未来更多的属性,所以我真的可以不要将每个属性“分配”给tmp
函数中的对象,以一种丑陋的方式解决这个问题......
在这两种情况下,用于循环返回的 List 的代码是这样的:
注意:请记住,使用第一个函数我无法解析method.hasbody
属性和method.body.instructions
属性,都抛出 NullReference 异常。
但是使用修改后的函数,只有在返回列表之前将其method.hasbody
分配给函数内部的变量时,我才能解析该属性,并且如果执行相同操作,则该属性也是如此。tmp
method.body.instructions
Dim methods As List(Of MethodDef) =
GetMethods("C:\WindowsApplication.exe", "Main")
For Each method As MethodDef In methods
MsgBox(method.Name)
If method.HasBody Then
Dim sb As New System.Text.StringBuilder
With sb
.AppendLine(String.Format("Method Instructions: {0}", Environment.NewLine &
String.Join(Environment.NewLine, method.Body.Instructions)))
End With
MsgBox(sb.ToString)
End If
Next method