I’m looking for ways to ‘publish’ a method in one interface to another interface, but hide it for others.
I have the following interfaces
public interface IFirst
{
void Forbidden();
}
public interface ISecond
{
void Test(IFirst first);
}
internal class Second : ISecond
{
void Test(IFirst first)
{
first.Forbidden();
}
}
As you can see, implementations of ISecond need to call method ‘Forbidden’ on IFirst. However, I do not want to allow classes in another assembly to call ‘Forbidden’ on implementations of IFirst. What can I do to hide this method from the outside world but still allowing implementations of ISecond to use it?
I don’t think there is anything you can do about this if you want to interfaces, as if you want a public method of
ISecondto take a parameter ofIFirstthenIFirstmust be public and so must all its methods, so users ofIFirstcould always call the methods.You could test the implementation of
IFirstyou are given to see if it implements another internal interface, but unless you are in control of dishing out the implementations ofIFirstthat are being passed in this is not guaranteed to work (and is not guaranteed to work even if you are in control of dishing out the instances) as someone could always pass in some other implementation which doesn’t implement the internal interface.You might be able to do it by using an abstract base class instead, but I haven’t tested this, just an idea: