X和Y的通用约束

时间:2015-06-18 17:17:02

标签: c# .net generics type-constraints

可以使用泛型约束对抽象类的类型派生实施约束,但只能使用实现接口的那些吗?

示例:

abstract class Dependency
{
    public abstract void IMustDoThis();
}

interface IOptionalDependency
{
    void IMightDoThis();
}

sealed class AlmightyDependency : Dependency, IOptionalDependency
{
    // I have sealed this for a reason!

    public override void IMustDoThis()
    {
        // I am almighty because I do this!
    }

    public void IMightDoThis()
    {
        // I am almighty because I can do this too!
    }
}

class ThisIsntTheAnswer : AlmightyDependency
{
    // AlmightyDependency is sealed...this is not the answer I'm looking for!
}

static class DoSomeAlmightyWork
{
    static void INeedToDoBoth<T>(T dependency) where T : Dependency ...AND... IOptionalDependency
    {
        dependency.IMustDoThis();
        if (something)
        {
            dependency.IMightDoThis();
        }
    }
}

有没有办法在C#中强制执行这样的依赖?

当前解决方案:

我目前的解决方案如下:

static void INeedToDoBoth(Dependency dependency, IOptionalDependency optional)
{
    dependency.IMustDoThis();
    if (something)
    {
        optional.IMightDoThis();
    }
}

但这意味着我将相同的参数传递两次,看起来很脏!

INeedToDoBoth(dependency, dependency);

我考虑过的另一个解决方法是:

static void INeedToDoBoth(IOptionalDependency optional)
{
    Dependency dependency = optional as Dependency;
    if(dependency != null)
    {
        dependency.IMustDoThis();
        // But if I MUST do this, and I was null...then what?
    }

    if (something)
    {
        optional.IMightDoThis();
    }
}

1 个答案:

答案 0 :(得分:7)

听起来你只是缺少在约束中指定类和接口作为逗号分隔列表的能力:

static void INeedToDoBoth<T>(T dependency)
    where T : Dependency, IOptionalDependency

请注意,类约束必须首先出现在这里。

有关详细信息,请参阅MSDN page for type parameter constraints或C#5规范部分10.1.5。