Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving Event name from lambda expression

Tags:

c#

lambda

is there way how to get name ov event from Lambda expression like with property ( Retrieving Property name from lambda expression ) ?

Thanks

like image 253
TcKs Avatar asked Mar 06 '10 10:03

TcKs


2 Answers

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.

like image 65
Enigmativity Avatar answered Oct 21 '22 21:10

Enigmativity


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

like image 30
Marc Gravell Avatar answered Oct 21 '22 21:10

Marc Gravell