特定のタイプのオブジェクトのプロパティのリストを取得したいので、この静的メソッドを作成してジョブを実行しました。
例: クラス A には 3 つの bool プロパティがあり、GetPropertiesList< bool>(aInstance) を呼び出します。bool を返すプロパティをすべて含むリストを返します。
それは大丈夫ですか、それともここで車輪を再発明していますか?
public static List<T> GetPropertiesList<T>(object obj)
{
var propList = new List<T>();
PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
//search
foreach (PropertyInfo prop in properties)
{
if (prop.PropertyType != typeof(T)) { continue; }
else
{
//Add to list
var foundProp = (T)prop.GetValue(obj, null);
propList.Add(foundProp);
}
}
return propList;
}