Let’s say I have an interface like that:
interface IAwesome
{
T DoSomething<T>();
}
Is there any way to implement the DoSomething method with type constraint? Obviously, this won’t work:
class IncrediblyAwesome<T> : IAwesome where T : PonyFactoryFactoryFacade
{
public T DoSomething()
{
throw new NotImplementedException();
}
}
This obviously won’t work because this DoSomething() won’t fully satisfy the contract of IAwesome – it only work for a subset of all possible values of the type parameter T. Is there any way to get this work short of some “casting black magic” (which is what I’m going to do as last resort if the answer is no)?
Honestly, I don’t think it’s possible but I wonder what you guys think.
EDIT: The interface in question is System.Linq.IQueryProvider so I can’t modify the interface itself.
No, this cannot work by design, since it would mean that the contract for
IAwesomewould not be (fully) satisfied.As long as
IncrediblyAwesome<T>implementsIAwesome, one is allowed to do this:Obviously, with your additional constraint, this could not work, since the user of
IAwesomecannot know of the restrictions put on it.In your case, the only solution I can think of is this (doing runtime checking):