I don't think you can force the use of one interface when using another, unless you derive one interface from the other. However, you could use attributes and set up a unit test that would check your code for you.
[MyInterfaceCheck(typeof(IMyBaseInterface))]
public interface IMyInterfaceA {...}
public interface IMyBaseInterface {...}
public interface IMyInterfaceB : IMyBaseInterface {...}
public class MyClass : IMyInterfaceA, IMyInterfaceB {...}
The attribute would be defined simply as:
public class MyInterfaceCheckAttribute : Attribute
{
public MyInterfaceCheckAttribute(Type typeThatShouldAlsoBeInherited)
{
if (!typeThatShouldAlsoBeInherited.IsInterface)
{
throw new ArgumentException("Incorrect type being used with MyInterfaceCheckAttribute.");
}
TypeThatShouldBeInherited = typeThatShouldAlsoBeInherited;
}
public Type TypeThatShouldBeInherited { get; private set; }
}
And the Unit Test:
[TestMethod]
public void CheckInterfaceInheritenceTest()
{
Dictionary<Type, MyInterfaceCheckAttribute> typesToCheck = new Dictionary<Type, MyInterfaceCheckAttribute>();
foreach (Type typeToCheck in Assembly.GetExecutingAssembly().GetTypes())
{
MyInterfaceCheckAttribute myAtt = typeToCheck.GetCustomAttribute(typeof(MyInterfaceCheckAttribute), true) as MyInterfaceCheckAttribute;
if (myAtt != null)
{
typesToCheck.Add(typeToCheck, myAtt);
}
}
foreach (Type typeToCheck in Assembly.GetExecutingAssembly().GetTypes())
{
Type[] interfaces = typeToCheck.GetInterfaces();
foreach (KeyValuePair<Type, MyInterfaceCheckAttribute> kvp in typesToCheck)
{
if (interfaces.Contains(kvp.Key) && !interfaces.Contains(kvp.Value.TypeThatShouldBeInherited))
{
Assert.Fail("The type " + typeToCheck.Name + " should inherit the interface " + kvp.Value.TypeThatShouldBeInherited.Name);
}
}
}
}