【C#】Type.GetProperties()メソッドでプロパティ情報を取得する方法
C#のType.GetProperties()メソッドは、リフレクション機能を利用して、現在のTypeオブジェクトが持つプロパティ情報を取得するために使用されます。このメソッドを使うことで、クラスや構造体などが公開しているプロパティの一覧を実行時に動的に調べることができます。
構文
Type.GetProperties()メソッドには、以下の2つのオーバーロードが用意されています。
public System.Reflection.PropertyInfo[] GetProperties (); public abstract System.Reflection.PropertyInfo[] GetProperties (System.Reflection.BindingFlags bindingAttr);
引数のbindingAttrには、検索の実行方法を指定する列挙値(BindingFlags)のビットごとの組み合わせを指定します。これにより、パブリック・非パブリック、静的・インスタンスといった条件で、取得するプロパティを柔軟に絞り込むことが可能です。
使用例1:System.Type型のプロパティを取得する
以下の例では、System.Type型自身が持つプロパティをGetProperties()メソッドで取得し、コンソールに一覧表示しています。
using System;
using System.Reflection;
public class Demo {
public static void Main(){
Type type = typeof(System.Type);
PropertyInfo[] info = type.GetProperties();
Console.WriteLine("Properties... ");
for (int i = 0; i < info.Length; i++)
Console.WriteLine(" {0}", info[i].ToString());
}
}
出力結果
上記のコードを実行すると、次のような出力が得られます。
Properties... System.Reflection.MemberTypes MemberType System.Type DeclaringType System.Reflection.MethodBase DeclaringMethod System.Type ReflectedType System.Runtime.InteropServices.StructLayoutAttribute StructLayoutAttribute System.Guid GUID System.Reflection.Binder DefaultBinder System.Reflection.Module Module System.Reflection.Assembly Assembly System.RuntimeTypeHandle TypeHandle System.String FullName System.String Namespace System.String AssemblyQualifiedName System.Type BaseType System.Reflection.ConstructorInfo TypeInitializer Boolean IsNested System.Reflection.TypeAttributes Attributes System.Reflection.GenericParameterAttributes GenericParameterAttributes Boolean IsVisible Boolean IsNotPublic Boolean IsPublic Boolean IsNestedPublic Boolean IsNestedPrivate Boolean IsNestedFamily Boolean IsNestedAssembly Boolean IsNestedFamANDAssem Boolean IsNestedFamORAssem Boolean IsAutoLayout Boolean IsLayoutSequential Boolean IsExplicitLayout Boolean IsClass Boolean IsInterface Boolean IsValueType Boolean IsAbstract Boolean IsSealed Boolean IsEnum Boolean IsSpecialName Boolean IsImport Boolean IsSerializable Boolean IsAnsiClass Boolean IsUnicodeClass Boolean IsAutoClass Boolean IsArray Boolean IsGenericType Boolean IsGenericTypeDefinition Boolean IsConstructedGenericType Boolean IsGenericParameter Int32 GenericParameterPosition Boolean ContainsGenericParameters Boolean IsByRef Boolean IsPointer Boolean IsPrimitive Boolean IsCOMObject Boolean HasElementType Boolean IsContextful Boolean IsMarshalByRef System.Type[] GenericTypeArguments Boolean IsSecurityCritical Boolean IsSecuritySafeCritical Boolean IsSecurityTransparent System.Type UnderlyingSystemType System.String Name System.Collections.Generic.IEnumerable`1[System.Reflection.CustomAttributeData] CustomAttributes Int32 MetadataToken
使用例2:string型のプロパティを取得する
続いて、string型に対してType.GetProperties()メソッドを使用する例を見てみましょう。この例では、取得したプロパティの総数も併せて表示しています。
using System;
using System.Reflection;
public class Demo {
public static void Main(){
Type type = typeof(string);
PropertyInfo[] info = type.GetProperties();
Console.WriteLine("Count of Properties = "+info.Length);
Console.WriteLine("Properties... ");
for (int i = 0; i < info.Length; i++)
Console.WriteLine(" {0}", info[i].ToString());
}
}
出力結果
上記のコードを実行すると、次のような出力が得られます。
Count of Properties = 2 Properties... Char Chars [Int32] Int32 Length
このように、string型には「Chars」と「Length」の2つのプロパティが存在することが確認できます。
まとめ
- Type.GetProperties()メソッドは、指定したTypeのプロパティ情報をPropertyInfo型の配列として返します。
- 引数なしで呼び出した場合は、パブリックなプロパティのみが取得されます。
- BindingFlagsを指定することで、非パブリックや静的なメンバーも検索対象に含めることができます。
-
C#のType.Equals()メソッドとは?使い方とサンプルコードを解説
C#のType.Equals()メソッドは、現在のTypeインスタンスが表す基盤となるシステム型が、指定されたObjectまたはTypeの基盤となるシステム型と同一であるかどうかを判定します。リフレクション処理などで型同士を比較したい場合に役立つメソッドです。 構文 public virtual bool Equals (Type o); public override bool Equals (object o); パラメータ o:現在のTypeの基盤となるシステム型と比較する対象となるオブジェクトまたは型を指定します。 戻り値 両方の基盤システム型が同じ場合は true、異なる場合は fa
-
C#のElementAt()メソッドの使い方をわかりやすく解説
ElementAt()は、C#のSystem.Linq名前空間に含まれる拡張メソッドの一つで、配列やリストなどのシーケンスから、指定したインデックス位置にある要素を取得・表示するために使用します。 基本的な使い方 まず、次のような文字列配列を用意します。 string[] arr = { One, Two, Three, Four, Five }; この配列からインデックス0の要素を取得したい場合は、ElementAt()メソッドにインデックス番号を引数として渡します。 arr.ElementAt(0); サンプルコード 以下は、ElementAt()メソッドを使って特定のインデックスの要素を