[C#]Type.FindMembersとか

Type.GetType({型名})で取得できるタイプの中身を調べるType.FindMembersで見つけたサンプルをちょっこっと加工したもの。

// ToSearch フィルター
public static bool ToSearch(MemberInfo objMemberInfo, Object? objSearch)
{
    // Compare the name of the member function with the filter criteria.
    return true; // 常にtrueなのでFilterはnull以外なら何でもいい。
}
// FindMembers:MemberTypes.xxxx
public static void GetMemberTypes(Type t, MemberTypes mt, List<string> list)
{
    string filter = "*"; // 実はnull以外なら何でもいい。
    var mems = t.FindMembers(mt,
        BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance,
        new MemberFilter(ToSearch),
        filter);
    if (mems.Length > 0)
    {
        for (int j = 0; j < mems.Length; j++)
        {
            list.Add($"{t} {mt}'s supported: {mems[j].ToString()}.");
        }
    }
    else
    {
        // list.Add($"{t} does not implement the {mt}'s {filter}.");
    }
}
// GetMemberTypesAll
public static void GetMemberTypesAll(Type t, List<string>list)
{
    if (!listA.Exists(x => x == t.FullName))
    {
        listA.Add(t.FullName);
        GetMemberTypes(t, MemberTypes.Constructor,list);
        GetMemberTypes(t, MemberTypes.Custom, list);
        GetMemberTypes(t, MemberTypes.Event, list);
        GetMemberTypes(t, MemberTypes.Field, list);
        GetMemberTypes(t, MemberTypes.Method, list);
        GetMemberTypes(t, MemberTypes.NestedType, list);
        GetMemberTypes(t, MemberTypes.Property, list);
        GetMemberTypes(t, MemberTypes.TypeInfo, list);
        //以下はオマケ。
        GetTypeInterfaces(t, list);
        GetMembers(t, list);
    }
}
// FindInterfaces
public static List<string> listA = new();
public static void GetTypeInterfaces(Type t, List<string> list)
{
    string filter = "*";
    TypeFilter myFilter = new TypeFilter(ToSearch);
    Type[] myInterfaces = t.FindInterfaces(myFilter, "*");
    if (myInterfaces.Length > 0)
    {
        list.Add($"{t} implements the interface {filter}.");
        for (int j = 0; j < myInterfaces.Length; j++)
        {
            list.Add($"> Interfaces supported: {myInterfaces[j]}.");
            //
            string iTypename = myInterfaces[j].ToString();
            Type? t2 = Type.GetType(iTypename);
            if (t2 != null)
            {
                if (!listA.Exists(x => x == t2.FullName))
                {
                    List<string> list2 = new List<string>();
                    GetMemberTypesAll(t2, list2);
                    list.AddRange(list2);
                    listA.Add(t2.FullName);
                }
            }
        }
    }
    else
    {
        // list.Add($"{t} does not implement the interface {filter}.");
    }
}
// GetMembers
public static void GetMembers(Type t, List<string> list)
{
    // Get the MemberInfo array.
    MemberInfo[] members = t.GetMembers();
    // Get and display the name and the MemberType for each member.
    list.Add($"Members of {t.Name} ({members.Length})");
    foreach (var member in members)
    {
        MemberTypes memberType = member.MemberType;
        list.Add($"> {member.Name}: {memberType}");
    }
}

をこんな感じで呼び出す。

// 「コンソール・アプリ(>NET Framework)」のC#は、newの型省略表記やNull容認mの指定等が未対応の為
// 「コンソール・アプリ」でプロジェクトを作成。
// プロジェクトへのCOM参照: Microsoft ActiveX Data Object 6.1 Library
// using System.Reflection;

// コンパイラが変数の型宣言に伴う処理を行うことで
// Type.GetType("ADODB.Connection")で情報を得られる様にする。
public static ADODB.Connection dummy = null;
public static ADODB.Recordset? recordset = null;
public static ADODB.Connection? connection = null;
public static ADODB.Field? field = null;
//
public static void Main(string[] args)
{
    Type? t = Type.GetType("ADODB.Connection");
    List<string> list = new ();
    GetMemberTypesAll(t, list);
    //結果表示
    Console.Write("\r\n" + string.Join("\r\n", list.ToArray()) + "\r\n");
}

実行してみると、

ADODB.Connection implements the interface *.
> Interfaces supported: ADODB._Connection.
ADODB._Connection Method's supported: System.String get_ConnectionString().
ADODB._Connection Method's supported: Void set_ConnectionString(System.String).
ADODB._Connection Property's supported: System.String ConnectionString.
ADODB._Connection implements the interface *.
> Interfaces supported: ADODB.Connection15.
ADODB.Connection15 Method's supported: System.String get_ConnectionString().
ADODB.Connection15 Method's supported: Void set_ConnectionString(System.String).
ADODB.Connection15 Property's supported: System.String ConnectionString.
ADODB.Connection15 implements the interface *.
> Interfaces supported: ADODB._ADO.
Members of _ADO (0)
Members of Connection15 (3)
> get_ConnectionString: Method
> set_ConnectionString: Method
> ConnectionString: Property
> Interfaces supported: ADODB._ADO.
Members of _Connection (3)
> get_ConnectionString: Method
> set_ConnectionString: Method
> ConnectionString: Property
> Interfaces supported: ADODB.Connection15.
> Interfaces supported: ADODB._ADO.
> Interfaces supported: ADODB.ConnectionEvents_Event.
Members of ConnectionEvents_Event (0)
Members of Connection (0)ts_Event (0)
Members of Connection (0)

ほとんど情報を得られなかった。


  • タグ:


コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA