palmis
palmis

Reputation: 45

C# specific visibility for some methods

I have two classes in a namespace and these classes need to call each other like this:

public class A
{
    void MethodVisibleToB() {}
}
public class B
{
    void MethodVisibleToA() {}
}

I want the methods to not be seen neither from inside the dll nor from outside. I wanted only class A to see B.MethodVisibleToA and only class B to see A.MethodVisibleToA.

I usually restrain visibility of a method with nested classes, but this time it is not possible as well as unnecessarily complex. Can you suggest a way to prevent this methods to be called from outside?

The options I have now are not that good:

Edit: I forgot to mention that the classes are in the same namespace in the same assembly

Solution: I took Mrinal Kamboj's suggestion and implemented the interfaces explicitly:

internal interface IA
{
    void VisibleByB();
}

internal interface IB
{
    void VisibleByA();
}

public class A : IA
{
    void IA.VisibleByB() { }
}

public class B : IB
{
    void IB.VisibleByA() { }
}

Event more pervert solution to a pervert problem: this will prevent the two interfaces and relative methods to be accessed from other classes and even subclasses:

public static class ClassAreNotMeantToBeUsedThisWay
{
    interface IA
    {
        void VisibleByB();
    }

    interface IB
    {
        void VisibleByA();
    }

    public class A : IA
    {
        void IA.VisibleByB() { }
    }

    public class B : IB
    {
        void IB.VisibleByA() { }
    }
}

Upvotes: 2

Views: 519

Answers (2)

Paul Kertscher
Paul Kertscher

Reputation: 9703

You can mark the methods as internal

Internal types or members are accessible only within files in the same assembly

See the following example:

public class A
{
    internal void MethodVisibleToB() {}
}

public class B
{
    A aInstance = new A();

    internal void MethodVisibleToA() 
    {
        aInstance.MethodVisibleToB(); // this can only be called by methods that are within the same assembly as B
    }
}

Addendum

If you need to access the method from the outside, you can use InternalsVisibleToAttribute in the assembly where A and B are located. Anyway, I'd recommend this for a very restricted scope only, e.g. (unit-)tests. Place this line in any .cs file in your assembly (without an namespace)

[assembly: System.InternalsVisibleTo("MyAssembly.Tests")]

Upvotes: 2

Mrinal Kamboj
Mrinal Kamboj

Reputation: 11482

Try using an Interface and implement Explicitly as follows:

interface InterfaceA
{
    void MethodVisibleToB();
}

interface InterfaceB
{
    void MethodVisibleToA();
}

public class A : InterfaceA
{
    void InterfaceA.MethodVisibleToB() { }
}
public class B : InterfaceB
{
    void InterfaceB.MethodVisibleToA() { }
}

Access as follows:

InterfaceA a = new A();
a.MethodVisibleToB();

InterfaceB b = new B();
b.MethodVisibleToA();

this way method would be available when wrapped up in an interface type, will not be available when not wrapped in an Interface type, but a class type

Upvotes: 2

Related Questions