Partager via


Comment obtenir des informations sur les types et les membres à l'aide de la réflexion

L’espace System.Reflection de noms contient de nombreuses méthodes pour obtenir des informations sur les types et leurs membres. Cet article illustre l’une de ces méthodes. Type.GetMembers Pour plus d’informations, consultez la vue d’ensemble de la réflexion.

Exemple :

L'exemple suivant utilise la réflexion pour obtenir des informations sur le type et les membres :

using System;
using System.Reflection;

class Asminfo1
{
    public static void Main()
    {
        Console.WriteLine ("\nReflection.MemberInfo");

        // Get the Type and MemberInfo.
        // Insert the fully qualified class name inside the quotation marks in the
        // following statement.
        Type MyType = Type.GetType("System.IO.BinaryReader");
        MemberInfo[] Mymemberinfoarray = MyType.GetMembers(BindingFlags.Public |
            BindingFlags.NonPublic | BindingFlags.Static |
            BindingFlags.Instance | BindingFlags.DeclaredOnly);

        // Get and display the DeclaringType method.
        Console.Write($"\nThere are {Mymemberinfoarray.Length} documentable members in ");
        Console.Write($"{MyType.FullName}.");

        foreach (MemberInfo Mymemberinfo in Mymemberinfoarray)
        {
            Console.Write("\n" + Mymemberinfo.Name);
        }
    }
}
Imports System.Reflection

Class Asminfo1
    Public Shared Sub Main()
        Console.WriteLine("\nReflection.MemberInfo")

        ' Get the Type and MemberInfo.
        ' Insert the fully qualified class name inside the quotation marks in the
        ' following statement.
        Dim MyType As Type = Type.GetType("System.IO.BinaryReader")
        Dim Mymemberinfoarray() As MemberInfo = MyType.GetMembers(BindingFlags.Public Or
            BindingFlags.NonPublic Or BindingFlags.Static Or
            BindingFlags.Instance Or BindingFlags.DeclaredOnly)

        ' Get and display the DeclaringType method.
        Console.Write($"\nThere are {Mymemberinfoarray.Length} documentable members in ")
        Console.Write($"{MyType.FullName}.")

        For Each Mymemberinfo As MemberInfo in Mymemberinfoarray
            Console.Write("\n" + Mymemberinfo.Name)
        Next
    End Sub
End Class

Voir aussi