I have an event Load
public delegate void OnLoad(int i);
public event OnLoad Load;
I subscribe to it with a method:
public void Go()
{
Load += (x) => { };
}
Is it possible to retrieve this method using reflection? How?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In this particular case you could, with reflection. However, in general, you can’t. Events encapsulate the idea of subscribers subscribing and unsubscribing – and that’s all. A subscriber isn’t meant to find out what other subscribers there are.
A field-like event as you’ve just shown is simply backed by a field of the relevant delegate type, with autogenerated add/remove handlers which just use the field. However, there’s nothing to say they have to be implemented like that. For example, an event can store its subscribers in an
EventHandlerList, which is efficient if you have several events in a class and only a few of them are likely to be subscribed to.Now I suppose you could try to find the body of the “add” handler, decompile it and work out how the event handlers are being stored, and fetch them that way… but please don’t. You’re creating a lot of work, just to break encapsulation. Just redesign your code so that you don’t need to do this.
EDIT: I’ve been assuming that you’re talking about getting the subscribers from outside the class declaring the event. If you’re inside the class declaring the event, then it’s easy, because you know how the event is being stored.
At that point, the problem goes from “fetching the subscribers of an event” to “fetching the individual delegates making up a multicast delegate” – and that’s easy. As others have said, you can call
Delegate.GetInvocationListto get an array of delegates… and then use theDelegate.Methodproperty to get the method that that particular delegate targets.Now, let’s look again at your subscription code:
The method that’s used to create the delegate here isn’t
Go… it’s a method created by the C# compiler. It will have an “unspeakable name” (usually with angle brackets) so will look something like this:Now, is that actually what you want to retrieve? Or were you really looking to find out which method performed the subscription, rather than which method was used as the subscribed handler?