class Program
{
static void Main(string[] args)
{
Expression<Func<string[], Poco>> exp = a => new Poco { MyProperty1 = a[0], MyProperty2 = a[1], MyProperty3 = a[2] };
var lambda = exp.Compile();
var output = lambda(new[] {"one", "two", "three"});
Console.WriteLine(output.MyProperty1);
}
}
class Poco
{
public string MyProperty1 { get; set; }
public string MyProperty2 { get; set; }
public string MyProperty3 { get; set; }
}
我對調用lambda的部分不感興趣,這只是爲了完整性。我完全失去了試圖瀏覽表達樹,這可能教我如何釣魚。鑑於這個lambda,我怎樣才能用表達式樹手動編寫它?
+1,我很好奇,是否有任何理由使用'Item'屬性超過'Expression.ArrayIndex'? –
@ default.kramer - 我不知道「Expression.ArrayIndex」。我已經更新了使用它的答案,所以謝謝! – Lee