本文整理匯總了VB.NET中System.Reflection.FieldInfo.GetFieldFromHandle方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET FieldInfo.GetFieldFromHandle方法的具體用法?VB.NET FieldInfo.GetFieldFromHandle怎麽用?VB.NET FieldInfo.GetFieldFromHandle使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Reflection.FieldInfo
的用法示例。
在下文中一共展示了FieldInfo.GetFieldFromHandle方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: FieldInfo_GetFieldFromHandle
' 導入命名空間
Imports System.Reflection
Public Class FieldInfo_GetFieldFromHandle
Public x As String
Public y As Char
Public a As Single
Public b As Integer
Public Shared Sub Main()
' Get the type of the FieldInfo_GetFieldFromHandle class.
Dim myType As Type = GetType(FieldInfo_GetFieldFromHandle)
' Get the fields of the FieldInfo_GetFieldFromHandle class.
Dim myFieldInfoArray As FieldInfo() = myType.GetFields()
Console.WriteLine(ControlChars.NewLine & _
"The field information of the declared" & _
" fields x, y, a, and b is:" & ControlChars.NewLine)
Dim myRuntimeFieldHandle As RuntimeFieldHandle
Dim i As Integer
For i = 0 To myFieldInfoArray.Length - 1
' Get the RuntimeFieldHandle of myFieldInfoArray.
myRuntimeFieldHandle = myFieldInfoArray(i).FieldHandle
' Call the GetFieldFromHandle method.
Dim myFieldInfo As FieldInfo = FieldInfo.GetFieldFromHandle(myRuntimeFieldHandle)
' Display the FieldInfo of myFieldInfo.
Console.WriteLine("{0}", myFieldInfo)
Next i
End Sub
End Class
示例2: Example
' 導入命名空間
Imports System.Reflection
' A generic class with a field whose type is specified by the
' generic type parameter of the class.
Public Class Test(Of T)
Public TestField As T
End Class
Public Class Example
Public Shared Sub Main()
' Get type handles for Test(Of String) and its field.
Dim rth As RuntimeTypeHandle = _
GetType(Test(Of String)).TypeHandle
Dim rfh As RuntimeFieldHandle = _
GetType(Test(Of String)).GetField("TestField").FieldHandle
' When a field belongs to a constructed generic type,
' such as Test(Of String), retrieving the field from the
' field handle requires the type handle of the constructed
' generic type. An exception is thrown if the type is not
' included.
Try
Dim f1 As FieldInfo = FieldInfo.GetFieldFromHandle(rfh)
Catch ex As Exception
Console.WriteLine("{0}: {1}", ex.GetType().Name, ex.Message)
End Try
' To get the FieldInfo for a field on a generic type, use the
' overload that specifies the type handle.
Dim fi As FieldInfo = FieldInfo.GetFieldFromHandle(rfh, rth)
Console.WriteLine(vbCrLf & "The type of {0} is: {1}", _
fi.Name, fi.FieldType)
' All constructions of Test(Of T) for which T is a reference
' type share the same implementation, so the same runtime
' field handle can be used to retrieve the FieldInfo for
' TestField on any such construction. Here the runtime field
' handle is used with Test(Of Object).
fi = FieldInfo.GetFieldFromHandle(rfh, _
GetType(Test(Of Object)).TypeHandle)
Console.WriteLine(vbCrLf & "The type of {0} is: {1}", _
fi.Name, fi.FieldType)
' Each construction of Test(Of T) for which T is a value type
' has its own unique implementation, and an exception is thrown
' if you supply a constructed type other than the one that
' the runtime field handle belongs to.
Try
fi = FieldInfo.GetFieldFromHandle(rfh, _
GetType(Test(Of Integer)).TypeHandle)
Catch ex As Exception
Console.WriteLine(vbCrLf & "{0}: {1}", ex.GetType().Name, ex.Message)
End Try
End Sub
End Class
輸出:
ArgumentException: Cannot resolve field TestField because the declaring type of the field handle Test`1[T] is generic. Explicitly provide the declaring type to GetFieldFromHandle. The type of TestField is: System.String The type of TestField is: System.Object ArgumentException: Type handle 'Test`1[System.Int32]' and field handle with decl aring type 'Test`1[System.__Canon]' are incompatible. Get RuntimeFieldHandle and declaring RuntimeTypeHandle off the same FieldInfo.