Lets say I have an interface like this:
public interface MyInterface
{
int Property1
{
get;
}
void Method1();
void Method2();
}
Is there any way to force the implementer of the interface to implement parts of it explicitly? Something like:
public interface MyInterface
{
int Property1
{
get;
}
explicit void Method1();
explicit void Method2();
}
Edit: As to why I care about whether the interface is implemented explicitly or not; it’s not like it matters as far as functionality goes, but it could be helpful to hide some unnecessary details from people using the code.
I’m trying to mimic multiple inheritance in my system, using this pattern:
public interface IMovable
{
MovableComponent MovableComponent
{
get;
}
}
public struct MovableComponent
{
private Vector2 position;
private Vector2 velocity;
private Vector2 acceleration;
public int Method1()
{
// Implementation
}
public int Method2()
{
// Implementation
}
}
public static IMovableExtensions
{
public static void Method1(this IMovable movableObject)
{
movableObject.MovableComponent.Method1();
}
public static void Method2(this IMovable movableObject)
{
movableObject.MovableComponent.Method2();
}
}
public class MovableObject : IMovable
{
private readonly MovableComponent movableComponent = new MovableComponent();
public MovableComponent MovableComponent
{
get { return movableComponent; } // Preferably hiddem, all it's methods are available through extension methods.
}
}
class Program
{
static void Main(string[] args)
{
MovableObject movableObject = new MovableObject();
movableObject.Method1(); // Extension method
movableObject.Method2(); // Extension method
movableObject.MovableComponent // Should preferably be hidden.
}
}
If the MovableComponent property was implemented explicitly, it would, in most cases, be hidden from whoever was using the class. Hope that explanation wasn’t too horrible.
No, it is not possible to force implementers to pick explicit or implicit implementation of an interface.