【发布时间】:2010-11-01 05:21:06
【问题描述】:
我需要一个具有两个类型约束的泛型函数,每个约束都继承自不同的基类。我知道如何用一种类型做到这一点:
void foo<T>() where T : BaseClass
但是,我不知道如何使用两种类型来做到这一点:
void foo<TOne, TTwo>() where TOne : BaseOne // and TTwo : BaseTwo ???
你是怎么做到的? (使用 .NET 2)
【问题讨论】:
我需要一个具有两个类型约束的泛型函数,每个约束都继承自不同的基类。我知道如何用一种类型做到这一点:
void foo<T>() where T : BaseClass
但是,我不知道如何使用两种类型来做到这一点:
void foo<TOne, TTwo>() where TOne : BaseOne // and TTwo : BaseTwo ???
你是怎么做到的? (使用 .NET 2)
【问题讨论】:
void foo<TOne, TTwo>()
where TOne : BaseOne
where TTwo : BaseTwo
更多信息在这里:
http://msdn.microsoft.com/en-us/library/d5x73970.aspx
【讨论】:
void foo<TOne, TTwo>() where TOne : Class where TTwo : Class where TOne != TTwo 所以基本上我们不知道 TOne 和 Ttwo 是什么,除了它们是引用类型但不能相同