【发布时间】:2021-10-08 05:35:58
【问题描述】:
我不确定这是否可能。
我有许多实现接口 IBar 的不同类,并且有接受几个值的构造函数。与其创建一堆几乎相同的方法,是否可以有一个通用方法来创建适当的构造函数?
private function GetFoo(Of T)(byval p1, byval p2) as List(Of IBar)
dim list as new List(Of IBar)
dim foo as T
' a loop here for different values of x
foo = new T(x,p1)
list.Add(foo)
' end of loop
return list
end function
我明白了:
'New' cannot be used on a type parameter that does not have a 'New' constraint.
【问题讨论】: