My colleague and I came across this when I looked at the delegate’s call list. If you create an event in say class X, then you can access the public methods of the small event from this class. But (and please, ignore things such as, for example, why you have open access to class members, this is not what we are asking for!), If we have an instance of class Y, and access to the event inside X, it cannot call any of the public methods, such as GetInvocationList () events. We wanted to know how it works. Here is a sample code (read the comments to understand what we mean):
public class X { public delegate void TestMethod(); public event TestMethod testMethod; private void rubbish() { // can access testMethod.GetInvocationList() fine here testMethod.GetInvocationList(); } } public class Y { public Y() { X x = new X(); x.testMethod += this.test; // here it says testMethod can only appear on the left hand side of += or -= // why is this? (ie the below line is invalid) x.testMethod.GetInvocationList(); } public void test() { } }
Out of curiosity, how do you achieve this, and what is the reason that this feature is available?
Many thanks amit
methods c #
user555265
source share