How to make public methods visible only in the class and class owning the object in C #?

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

+8
methods c #
source share
4 answers

What the keyword event does; it is a modifier that restricts operations other than subscribing to a custom class. If you remove the event keyword, you get a simple delegate that clients outside the class can call, for example. method GetInvocationList() .

In a blog post, they compare the generated IL code for a simple delegate and event and are handled exactly the same. The event keyword is a compile-time modifier that restricts access to delegate methods. (It also allows use in interfaces). All details are given in the blog post.

+9
source share

I found some information about this - access to this outside the declaring class is very limited - below from the MSDN ( Event Tutorial ) site.

An event can be called only inside the class that declared the event. Attaching to an event outside the class that declared it, the event looks like a field, but access to this field is very limited. The only thing you can do:

Create a new delegate in this field.

Remove a delegate from a (possibly compound) field.

This is done using the + = and - = operators. To start receiving the invocations event, the client code first creates a delegate of the type of event that refers to the method that should be called from the event. then it combines the delegate with other delegates that the event may be associated with the use of + =.

Additional information is available here.

+1
source share

afaik is only when using events, if you delete the "event" in the line, you can use the method just fine

0
source share

Think of delegates separately from events

hmmm ... I'm going to take the wag here.

Place the delegate declaration in your namespace, outside of any classes.

  • In class X, create an instance of this delegate (do not read the event) and register "handlers:" (I use this term weakly) as desired.

  • Create a public method or property that can receive the delegate class Y (reference to) and therefore a list of calls.

  • Now you can do whatever you want in class Y. You can even create a “mirror” event in class Y using a call list of delegates of class X.

0
source share

All Articles