Straight to the problem: I have a class that implements two interfaces:
public class A : Interface1, Interface2{
// Interface 1 implementation...
// Interface 2 implementation...
}
Is there a way (without creating another new class) to make Interface1 implementation internal and hide it from other components (only Interface2 will stay public)?
EDIT: Some more useful infos: Interface1 and Interface2 are defined as public in another core component and cannot be changed.
Thanks in advance,
While you can make the interface itself internal, the methods would still be part of the public API. What you can elect to do is explicit interface implementation, so that the API defined by the interface is only visible via the interface, and not via the class.
Beyond this, if you need the world to never have any idea that the class supports the interface, then you would simply need to not have the class implement the interface. Interfaces are for the intent of broadcasting that a given object supports given behaviors, explicitly or otherwise. If that’s not what you want, you need to go a different direction. It could simply be that the class implements the behaviors as private implementation details, sans interface. Another approach is to shovel those implementations into a private nested class.
Under this approach, the world never knows that a class fulfills the interface contract, because it technically doesn’t. The contract is fulfilled by
Foo, and the world cannot seeFoo. However, the benefit is that if the class needs to invoke externally defined methods that require the interface, it can still pass the nested class instance to those methods.