FieldInfo.FieldHandle Propiedad
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Obtiene una estructura RuntimeFieldHandle
, que es un identificador para la representación interna de los metadatos de un campo.
public:
abstract property RuntimeFieldHandle FieldHandle { RuntimeFieldHandle get(); };
public abstract RuntimeFieldHandle FieldHandle { get; }
member this.FieldHandle : RuntimeFieldHandle
Public MustOverride ReadOnly Property FieldHandle As RuntimeFieldHandle
Identificador para la representación interna de metadatos de un campo.
En el ejemplo siguiente se recupera la información del campo MyClass.MyField y se muestra el campo asociado al identificador de campo.
using namespace System;
using namespace System::Reflection;
public ref class MyClass
{
public:
String^ MyField;
};
void DisplayFieldHandle( RuntimeFieldHandle myFieldHandle )
{
// Get the type from the handle.
FieldInfo^ myField = FieldInfo::GetFieldFromHandle( myFieldHandle );
// Display the type.
Console::WriteLine( "\nDisplaying the field from the handle.\n" );
Console::WriteLine( "The type is {0}.", myField );
}
int main()
{
MyClass^ myClass = gcnew MyClass;
// Get the type of MyClass.
Type^ myType = MyClass::typeid;
try
{
// Get the field information of MyField.
FieldInfo^ myFieldInfo = myType->GetField( "MyField", static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance) );
// Determine whether or not the FieldInfo Object* is 0.
if ( myFieldInfo != nullptr )
{
// Get the handle for the field.
RuntimeFieldHandle myFieldHandle = myFieldInfo->FieldHandle;
DisplayFieldHandle( myFieldHandle );
}
else
{
Console::WriteLine( "The myFieldInfo Object* is 0." );
}
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception: {0}", e->Message );
}
}
using System;
using System.Reflection;
public class MyClass
{
public string MyField = "Microsoft";
}
public class FieldInfo_FieldHandle
{
public static void Main()
{
MyClass myClass =new MyClass();
// Get the type of MyClass.
Type myType = typeof(MyClass);
try
{
// Get the field information of MyField.
FieldInfo myFieldInfo = myType.GetField("MyField", BindingFlags.Public
| BindingFlags.Instance);
// Determine whether or not the FieldInfo object is null.
if(myFieldInfo!=null)
{
// Get the handle for the field.
RuntimeFieldHandle myFieldHandle=myFieldInfo.FieldHandle;
DisplayFieldHandle(myFieldHandle);
}
else
{
Console.WriteLine("The myFieldInfo object is null.");
}
}
catch(Exception e)
{
Console.WriteLine("Exception: {0}", e.Message);
}
}
public static void DisplayFieldHandle(RuntimeFieldHandle myFieldHandle)
{
// Get the type from the handle.
FieldInfo myField = FieldInfo.GetFieldFromHandle(myFieldHandle);
// Display the type.
Console.WriteLine("\nDisplaying the field from the handle.\n");
Console.WriteLine("The type is {0}.", myField.ToString());
}
}
Imports System.Reflection
Public Class [MyClass]
Public MyField As String = "Microsoft"
End Class
Public Class FieldInfo_FieldHandle
Public Shared Sub Main()
Dim [myClass] As New [MyClass]()
' Get the type of MyClass.
Dim myType As Type = GetType([MyClass])
Try
' Get the field information of MyField.
Dim myFieldInfo As FieldInfo = myType.GetField("MyField", BindingFlags.Public Or BindingFlags.Instance)
' Determine whether or not the FieldInfo object is null.
If Not (myFieldInfo Is Nothing) Then
' Get the handle for the field.
Dim myFieldHandle As RuntimeFieldHandle = myFieldInfo.FieldHandle
DisplayFieldHandle(myFieldHandle)
Else
Console.WriteLine("The myFieldInfo object is null.")
End If
Catch e As Exception
Console.WriteLine(" Exception: {0}", e.Message.ToString())
End Try
End Sub
Public Shared Sub DisplayFieldHandle(ByVal myFieldHandle As RuntimeFieldHandle)
' Get the type from the handle.
Dim myField As FieldInfo = FieldInfo.GetFieldFromHandle(myFieldHandle)
' Display the type.
Console.WriteLine(ControlChars.Cr + "Displaying the field from the handle." + ControlChars.Cr)
Console.WriteLine("The type is {0}.", myField.ToString())
End Sub
End Class
Los identificadores solo son válidos en el appdomain en el que se obtuvieron.
Produto | Versións |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
Comentarios de .NET
.NET é un proxecto de código aberto. Selecciona unha ligazón para ofrecer comentarios: