FieldInfo::GetFieldFromHandle Method (RuntimeFieldHandle, RuntimeTypeHandle)
Gets a FieldInfo for the field represented by the specified handle, for the specified generic type.
Assembly: mscorlib (in mscorlib.dll)
public: [ComVisibleAttribute(false)] static FieldInfo^ GetFieldFromHandle( RuntimeFieldHandle handle, RuntimeTypeHandle declaringType )
Parameters
- handle
-
Type:
System::RuntimeFieldHandle
A RuntimeFieldHandle structure that contains the handle to the internal metadata representation of a field.
- declaringType
-
Type:
System::RuntimeTypeHandle
A RuntimeTypeHandle structure that contains the handle to the generic type that defines the field.
Return Value
Type: System.Reflection::FieldInfo^A FieldInfo object representing the field specified by handle, in the generic type specified by declaringType.
| Exception | Condition |
|---|---|
| ArgumentException | handle is invalid. -or- declaringType is not compatible with handle. For example, declaringType is the runtime type handle of the generic type definition, and handle comes from a constructed type. See Remarks. |
Handles are valid only in the application domain in which they were obtained.
The recommended practice is that declaringType should always be the runtime type handle of the constructed type that handle belongs to. That is, if handle is a runtime field handle for a field that belongs to MyType<int> (MyType(Of Integer) in Visual Basic), declaringType is the runtime type handle for MyType<int>. Do not use the runtime type handle of the generic type definition, unless the runtime field handle represents a field on the generic type definition.
Implementations are compatible in some cases. For example, a single implementation is shared by all types that are constructed from a particular generic type definition by using reference types for the generic type arguments. For example, MyType<string>, MyType<object>, and MyType<ArrayList> all share the same implementation. In this situation, the FieldInfo object that is returned represents a field on the type that declaringType specifies, regardless of the original source of handle. This practice is not recommended, because it works only if the generic type arguments of the constructed type are reference types.
If a generic argument is a value type, the runtime type handle of the constructed type is not compatible with runtime field handles from constructions that have a reference type in the same generic parameter position, or that have a different value type in that position. In that case, the only way to use the FieldInfo::GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle) overload is to ensure that declaringType is the runtime type handle for the constructed type that handle belongs to.
The following example shows how to retrieve FieldInfo objects for fields on constructed generic classes. The example defines the generic type Test<T> (Test(Of T) in Visual Basic) with a single field named TestField, of type T. The example gets the RuntimeFieldHandle and RuntimeTypeHandle for the case where T is String, and demonstrates the following:
An exception is thrown if the GetFieldFromHandle(RuntimeFieldHandle) method overload is used. This is true even if the field is not of type T.
A FieldInfo is retrieved successfully if the runtime type handle is from the same construction as the runtime field handle, in this case Test<string>.
If the runtime type handle is from a compatible construction, in this case Test<object>, a FieldInfo for the field on the compatible construction is retrieved.
If the runtime type handle is not from a compatible construction, an exception is thrown. In this case, a value type is specified for T.
when invoked late-bound through mechanisms such as Type::InvokeMember. Associated enumeration: ReflectionPermissionFlag::MemberAccess.
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1