Is it any way to make an interface implementation private?
Not completely private - an interface represents a public set of methods and properties. There is no way to make interface implementations private.
What you can do is make the implementation explicit:
public interface IFoo
{
void Bar();
}
public class FooImpl
{
void IFoo.Bar()
{
Console.WriteLine("I am somewhat private.")
}
private void Bar()
{
Console.WriteLine("I am private.")
}
}
Now the only way to call IFoo.Bar()
is explicitly through the interface:
FooImpl f = new FooImpl();
f.Bar(); // compiler error
((IFoo)f).Bar();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…