开发者

Retrieving Event name from lambda expression

开发者 https://www.devze.com 2022-12-22 15:17 出处:网络
is there way how to开发者_Go百科 get name ov event from Lambda expression like with property ( Retrieving Property name from lambda expression ) ?

is there way how to开发者_Go百科 get name ov event from Lambda expression like with property ( Retrieving Property name from lambda expression ) ?

Thanks


Yes, it's just like getting the property name, but you must do it in the class that defines the event.

public class Foo
{
    public event EventHandler Bar;

    public string BarName
    {
        get
        {
            return this.GetEventName(() => this.Bar);
        }
    }

    private string GetEventName(Expression<Func<EventHandler>> expression)
    {
        return (expression.Body as MemberExpression).Member.Name;
    }
}

Enjoy.


No. C# lambdas don't support events, so there is no way of representing this. You'll have to use reflection.

0

精彩评论

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

关注公众号