I have a specific problem where I have an interface IA
that has a method Foo
that returns another interface IB
. I want to be able to have two interfaces IA1
(that implements IA
) and IB1
(that implements IB
), and have IA.Foo
return IB1
. I'd like for this to happen with any other interface that implements IA
(for example, some IA2
).
I think the only possible way to do this is via higher-order-types, which would look like the "solution" below (which is not valid C#):
public interface IA<M> where M<> : IB<>
{
M<T> Foo1<T>(T val);
M<T> Foo2<T>();
}
public interface IB<T>
{
T Bar();
}
public interface IA1 : IA<IB1> {}
public interface IB1<T> : IB<T>
{
void Bar1();
}
public interface IA2 : IA<IB2> {}
public interface IB2<T> : IB<T>
{
void Bar2(T val);
}
public void MyFunc()
{
IA1 myA1 = GetMyA1();
IB1<int> myB1Int = myA1.Foo1<int>(2);
int myB1IntBar = myB1Int.Bar();
myB1Int.Bar1();
IB1<string> myB1String = myA1.Foo2<string>();
string myB1StringBar = myB1String.Bar();
IA2 myA2 = GetMyA2();
IB2<string> myB2 = myA2.Foo1<string>("Hello World");
string myB2StringBar = myB2.Bar();
myB2.Bar2("Value");
}
Is there a way to simulate this in C#?