Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello friends, if I have a class with this definition:

C#
public class Example
{
    public void HelloWorld()
    {
        Debug.Print("Hello World!!");
    }
}


Now consider that I have a collection of this:

C#
List<Example> Examples = new List<Example>();


What I want to do is, don't allow the HelloWord method to be accessed from the collection like this:

C#
Examples(0).HelloWorld();


But that can be accessed from the object like this:

C#
Example e = Examples(0);
e.HelloWorld();


It is possible?

What I have tried:

I tried using reflection to solve this but didn't find anything that helped me.
Posted
Updated 19-Aug-21 4:06am
Comments
Richard MacCutchan 19-Aug-21 7:40am    
If a method is public then it can be called on any object of that class.

You can't completely prevent the method from being accessed. But you could create an interface or base class which doesn't include the method, and change your collection to use that instead.
C#
public interface IExample
{
    void SayHello();
    void GoodbyeWorld();
}

public class Example : IExample
{
    public void HelloWorld()
    {
        Console.WriteLine("Bonjour tous le monde!");
    }
    
    public void SayHello()
    {
        Console.WriteLine("Kon'nichiwa!");
    }
    
    void IExample.GoodbyeWorld()
    {
        Console.WriteLine("Tot ziens iedereen");
    }
}

List<IExample> Examples = new List<IExample>
{
    new Example()
};
Usage:
C#
Examples[0].HelloWorld();   // Won't compile
Examples[0].SayHello();     // Works
Examples[0].GoodbyeWorld(); // Works

Example e = new Example();
e.HelloWorld();   // Works
e.SayHello();     // Works
e.GoodbyeWorld(); // Won't compile
However, you can still access the "won't compile" methods with an explicit cast:
C#
((IExample)e).GoodbyeWorld();        // Works
((Example)Examples[0]).HelloWorld(); // Works, but could throw exception at runtime
interface - C# Reference | Microsoft Docs[^]
 
Share this answer
 
No, this is not possible.

While you might see
C#
Example e = Examples[0];
e.HelloWorld();


C#
Examples[0].HelloWorld();


as being two very different things - they are not. They mean the exact same thing to the compiler and to anyone familiar with C#. If you managed to break this expected behavior your code would become unreadable. Always try to avoid being "smart" like this, it will come back and kick you!

I suspect you have a different problem, then thought of this as the solution and are now asking for help to implement this solution (a so-called XY problem). Maybe tell us why you are needing this, and we can tell you the best way to achieve what you are really trying to do.
 
Share this answer
 
You can't. Both of your code snippets do the exact same thing!

You cannot filter out access to a method or class based on how the code calling it was written.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900