【发布时间】:2013-06-20 18:20:03
【问题描述】:
考虑以下场景:
Public Class Condition(Of T)
'...
End Class
现在,我创建了这个类的几个不同类型的实例。我现在希望用这些实例填充一个通用列表。比如:
Dim Conditions As New List(Of Condition....?)
Conditions.add(new Condition(Of String))
Conditions.add(new Condition(Of Double))
Conditions.add(new Condition(Of CustomClass))
最好的方法是什么?现在我正在做以下事情:
Public Class Condition(Of T)
implements ICondition
'...
End Class
然后像这样使用它:
Dim Conditions As New List(Of ICondition)
Conditions.add(new Condition(Of String))
Conditions.add(new Condition(Of Double))
Conditions.add(new Condition(Of CustomClass))
但是,ICondition 完全是空的。这让我觉得可能有更好或更被接受的方式来处理事情。我也可以从一个空的基类继承,那会更好吗?做这种事情的首选方法是什么?
我了解,只要包含泛型的列表属于更广泛的类型,我就可以插入任何类型的条件。但是我不想让它超出必要的范围 - 我只想能够插入作为 Public Class Condition(Of T) 实例的对象,但其类型可能会有所不同。
【问题讨论】:
标签: vb.net generics interface abstraction