私は動的表現を探求し始めているので、1 つの問題を解決するのを手伝ってください。私はオブジェクトを持っています
public class Categorisation{
string Name{get;set;}
}
public class Client{
public Categorisation Categorisation{get;set;}
}
必要なのは、動的な式を記述して Categorisation を呼び出すことだけです。Client オブジェクトから .Equals("A1") に名前を付けます。
x=>x.Categorisation.Name.Equals("A1")
式を使用してこれを行うにはどうすればよいですか?
var param = Expression.Parameter(typeof(Client));
var prop = Expression.Property(param, typeof(Client).GetProperty("Categorisation"));
var argument = Expression.Constant("A1");
var method = typeof(string).GetMethod("Equals", new[] { typeof(string) });
var call = Expression.Call(prop, method);
var expr = Expression.Lambda<Func<Client, bool>>(call, param);
もちろん、このコードは間違っており、メソッド Equals を分類プロパティから呼び出します。分類の名前からではありません。しかし、Name プロパティを呼び出す方法は?