C# 多个泛型约束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1176908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:06:17  来源:igfitidea点击:

C# Multiple generic constraints

c#constraintsgenerics

提问by Patrick

I was wondering if it is possible to add multiple generic constraints?

我想知道是否可以添加多个通用约束?

I have an Add method that takes an Object (Either Email, Phone or Address), so i was thinking something like:

我有一个接受对象(电子邮件、电话或地址)的 Add 方法,所以我在想:

public void Add<T>(T Obj) 
    where T : Address
    where T : Email
    where T : Phone
{
    if (Obj is Address)
        m_Address.Add(Obj as Address);
    else if (Obj is Email)
        m_Email.Add(Obj as Email);
    else
        m_Phone.Add(Obj as Phone);
}

But I keep getting:

但我不断得到:

"A constraint clause has already been specified for type parameter 'T'. All of the constraints for a type parameter must be specified in a single where clause."

"A constraint clause has already been specified for type parameter 'T'. All of the constraints for a type parameter must be specified in a single where clause."

采纳答案by Greg Beech

You can't do that. Why not just have three methods and let the compiler do the hard work for you?

你不能那样做。为什么不只拥有三个方法并让编译器为您完成繁重的工作呢?

public void Add(Address address) { m_Address.Add(address); }
public void Add(Email email) { m_Email.Add(email); }
public void Add(Phone phone) { m_Phone.Add(phone); }

回答by Lucas Jones

In that case, I wouldn't bother, as you are comparing types anyway. Use this:

在那种情况下,我不会打扰,因为您无论如何都在比较类型。用这个:

public void Add(object Obj)
{
    if (Obj is Address)
        m_Address.Add(Obj as Address);
    else if (Obj is Email)
        m_Email.Add(Obj as Email);
    else if (Obj is Phone)
        m_Phone.Add(Obj as Phone);
    else
        return;
}

I don't think multiple clauses are supported. You could also have separate method overloads too.

我认为不支持多个条款。您也可以有单独的方法重载。

回答by Adrian Godong

How about creating an interface or base class for those three types?

为这三种类型创建接口或基类怎么样?

But looking at your code, seems that you're not using generic well enough. The point of using generic is that you don't need to cast it into any particular type (in this case, you are).

但是看看你的代码,你似乎没有很好地使用泛型。使用泛型的要点是您不需要将其转换为任何特定类型(在这种情况下,您是)。

回答by Anton Gogolev

CLR does not allow multiple inheritance, which is precisely what you're trying to express. You want Tto be Address, Emailan Phoneat the same time (I assume those are class names). Thus is impossible. What's event more, this whole method makes no sense. You'll either have to introduce a base interface for all three classes or use three overloads of an Addmethod.

CLR 不允许多重继承,这正是您要表达的意思。你想T成为AddressEmail一个Phone在同一时间(我认为这些都是类名)。因此是不可能的。更重要的是,这整个方法毫无意义。您要么必须为所有三个类引入一个基接口,要么使用一个Add方法的三个重载。

回答by bruno conde

You don't get any real benefits from generics in this case. I would just create different Add methods for each parameter Type.

在这种情况下,您不会从泛型中获得任何真正的好处。我只会为每个参数类型创建不同的 Add 方法。

回答by Evan

where T : C1, C2, C3. Comma separated for constraints. Try using Base class or interfaces.

其中 T:C1、C2、C3。逗号分隔约束。尝试使用基类或接口。

回答by user3664942

Like others have said, in your specific case you should use inheritance or method overloading instead of generics. However, if you do need to create a generic method with multiple constraints then you can do it like this.

就像其他人所说的那样,在您的特定情况下,您应该使用继承或方法重载而不是泛​​型。但是,如果您确实需要创建具有多个约束的泛型方法,那么您可以这样做。

public void Foo<T>() where T : Bar, IBaz, new()
{
    // Your code here
}