简体   繁体   中英

Type constraints on implementations of generic members of non-generic interfaces in C#

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 IAwesome would not be (fully) satisfied.

As long as IncrediblyAwesome<T> implements IAwesome , one is allowed to do this:

IAwesome x = new IncrediblyAwesome<Something>()

Obviously, with your additional constraint, this could not work, since the user of IAwesome cannot know of the restrictions put on it.

In your case, the only solution I can think of is this (doing runtime checking):

interface IAwesome { // assuming the same interface as in your sample
    T DoSomething<T>();
}

class IncrediblyAwesome<TPony> : IAwesome where TPony : PonyFactoryFactoryFacade {
    IAwesome.DoSomething<TAnything>() {
        return (TAnything)((object)DoSomething()); // or another conversion, maybe using the Convert class
    }

    public TPony DoSomething() {
        throw new NotImplementedException();
    }
}

Wouldn't something like this do the trick?

interface IAwesome<U>
{
    T DoSomething<T>() where T : U
}

class IncrediblyAwesome<T> : IAwesome<PonyFactoryFactoryFacade>
{
    public T DoSomething()
    {
        throw new NotImplementedException();
    }
}

I'm not sure if this compiles.

As you've already mentioned, such a solution cannot work.

Moreover, your example violates the contract in another way: One instance of IncrediblyAwesome is always bound to one specific descendant of PonyFactoryFactoryFacade (btw, I'd be really interested in the purpose of this class :-)). Therefore, the concrete implementation of DoSomething would not be a generic method as specified in the interface, but always return one single type. You couln't write:

IAwesome a = new IncrediblyAwesome<SomePonyFacadeFactory1>();
SomePonyFacadeFactory2 facade2 = a.DoSomething<SomePonyFacadeFactory2>();

even though both facade-factories decend from PonyFactoryFactoryFacade ...

Another comment: I would stay away from black magic casting, since this problems are inherent to your design and not just a shortcoming of C# ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM