私がやりたいことは、どのクラスタイプでも、オブジェクトグラフのすべてのプロパティに「取得」アクセサのリストを作成することです。プログラムでプロパティアクセサファンクションのコレクションを作成する
コレクションの形式、順序などは問題ではありません。すべてのプロパティのアクセサーを識別して作成する方法はわかりません。
public static List<Func<T,object>> CreateAccessors<T>()
{
Type t = typeof(T);
// Identify all properties and properties of properties (etc.) of T
// Return list of lambda functions to access each one given an instance of T
}
public void MyTest()
{
MyClass object1;
var accessors = CreateAccessors<MyClass>();
var myVal1 = accessors[0](object1);
var myVal2 = accessors[1](object1);
// myVal1 might now contain the value of object1.Property1
// myVal2 might now contain the value of object1.Property4.ThirdValue.Alpha
}
Jon Skeetのこのブログ記事 - https://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspxを見てください多くの場合、再帰を禁止します。 –
あなたもこれを見たいと思うかもしれません。http://stackoverflow.com/questions/16578857/using-nested-collections-of-lambda-expressions-to-create-an-object-graph – nawfal
JonのMVPブログがなくなりました。彼の個人的なブログの投稿のコピー: https://codeblog.jonskeet.uk/2008/08/09/making-reflection-fly-and-exploring-delegates/ –