FieldInfo.FieldHandle Propriété

Définition

Obtient RuntimeFieldHandle qui est un handle vers la représentation interne des métadonnées d'un champ.

C#
public abstract RuntimeFieldHandle FieldHandle { get; }

Valeur de propriété

Handle vers la représentation interne des métadonnées d'un champ.

Implémente

Exemples

L’exemple suivant récupère les informations de champ MyClass.MyField et affiche le champ associé au handle de champ.

C#
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());
    }
}

Remarques

Les handles sont valides uniquement dans le domaine d’application dans lequel ils ont été obtenus.

S’applique à

Produit Versions
.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