Is it possible to forcefully implement an interface (or parts of it)?

Say I have an interface like this:

public interface MyInterface { int Property1 { get; } void Method1(); void Method2(); } 

Is there a way to get an interface developer to implement it explicitly? Sort of:

 public interface MyInterface { int Property1 { get; } explicit void Method1(); explicit void Method2(); } 

Edit: As for why I care about whether the interface is implemented explicitly or not; this is not so important as the functionality goes, but it may be useful to hide some unnecessary data from people using the code.

I am trying to simulate multiple inheritance on 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 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, in most cases it would be hidden from the person who used this class. I hope this explanation was not too terrible.

+6
source share
2 answers

No, it is impossible to force developers to choose an explicit or implicit implementation of an interface.

+8
source

What you are looking for is called an abstract class.

0
source

All Articles