简体   繁体   English

C#多个通用约束

[英]C# Multiple generic constraints

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方法,它接受一个Object(电子邮件,电话或地址),所以我想的是:

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."

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); }

CLR does not allow multiple inheritance, which is precisely what you're trying to express. CLR不允许多重继承,这正是您要表达的内容。 You want T to be Address , Email an Phone at the same time (I assume those are class names). 您希望T同时为AddressEmail发送Phone (我假设这些是类名)。 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 Add method. 您要么必须为所有三个类引入基接口,要么使用Add方法的三个重载。

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). 使用泛型的关键是你不需要将它转换为任何特定的类型(在这种情况下,你是)。

You don't get any real benefits from generics in this case. 在这种情况下,您不会从泛型中获得任何实际好处。 I would just create different Add methods for each parameter Type. 我只想为每个参数Type创建不同的Add方法。

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
}

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. 您也可以使用单独的方法重载。

where T : C1, C2, C3. 其中T:C1,C2,C3。 Comma separated for constraints. 以逗号分隔约束。 Try using Base class or interfaces. 尝试使用Base类或接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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