基本クラスのIEnumerableをして子クラスでプロパティを取得します。私は特定のエンティティのコレクションを持っている場合、私はそうのようなエンティティのプロパティを取得することができるよ
var myCollection = new List<Foo>();
entities.GetType().GetGenericArguments()[0].GetProperties().Dump();
はしかし、私はいくつかを持っています私のコレクションが基本クラスのIEnumerableであり、派生クラスが実装されている場合、プロパティをリストすることが困難です。
public class Foo
{
public string One {get;set;}
}
public class Bar : Foo
{
public string Hello {get;set;}
public string World {get;set;}
}
// "Hello", "World", and "One" contained in the PropertyInfo[] collection
var barCollection = new List<Bar>() { new Bar() };
barCollection.GetType().GetGenericArguments()[0].GetProperties().Dump();
// Only "One" exists in the PropertyInfo[] collection
var fooCollection = new List<Foo>() { new Bar() };
fooCollection.GetType().GetGenericArguments()[0].GetProperties().Dump();
コレクションが基本クラスを使用して宣言されていても、コレクション内のアイテムの型を取得する方法はありますか?あなたがFoo
で、Foo
だけOne
プロパティを持つ型パラメータT
に代表されるタイプからプロパティを取得しているためである
:
可能なすべてのプロパティを取得するには、このようなリスト内のすべてのオブジェクトの種類を通過する必要があります時間情報。 –
リストに異なる種類のオブジェクトがある場合はどうなりますか?なぜあなたはそのプロパティが必要ですか? –