开发者

Evaluate a lambda expression

I have a class

public class Person
{
    public int PersonId { get; set; }
    public string Name { get; set; }
}

And I have a lambda expression of the Person type

Expression<Func<TModel, TProperty>> expression

Who contains t开发者_开发知识库his value

{model => model.Name}

How can I evaluate that lambda expression against an instance of Person, to extract the Name value attribute?


You can compile the expression into a delegate and pass in a Person object:

Func<Person, string> getName = expression.Compile();
string name = getName(person);


Using Expression trees:

  • http://msdn.microsoft.com/en-us/library/bb397951.aspx
  • http://blogs.msdn.com/b/charlie/archive/2008/01/31/expression-tree-basics.aspx
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜