Property Name to Lambda Expression C#

后端 未结 2 452
鱼传尺愫
鱼传尺愫 2021-01-04 14:21

How can I convert a property name to Lambda expression in C#?

Like this: string prop = \"Name\"; to (p => p.Name)

publi         


        
2条回答
  •  北荒
    北荒 (楼主)
    2021-01-04 14:45

    A lambda is just an anonymous function. You can store lambdas in delegates just like regular methods. I suggest you try making "Name" a property.

    public string Name { get { return p.Name; } }

    If you really want a lambda, use a delegate type such as Func.

    public Func Name = () => p.Name;

提交回复
热议问题