asp.net - 传递通用列表

chr*_*ris 3 vb.net asp.net generics collections

我有一个实用程序类,它将通用列表作为参数.

代码如下:

Function DoStuff(collection as Object, elt as Object)
   ...
   collection.Add(elt)
   ...
End Function
Run Code Online (Sandbox Code Playgroud)

这称为:

DoStuff( List(Of Foo), new Foo() )
DoStuff( List(Of Bar), new Bar() )
Run Code Online (Sandbox Code Playgroud)

有大约十几种不同的类型.

目前,传递为Object导致后期绑定分辨率警告,尽管它运行正常.

我已经尝试了不同的方式来传递集合和elt(Foo和Bar都扩展了一个基类)但似乎无法找出"正确"的方法来实现它.

想法?

wom*_*omp 7

我想你正在寻找这样的东西.

Public Sub DoStuff(Of T)(collection As List(Of T), elt As T)
    ...
    collection.Add(elt)
    ...
End Function
Run Code Online (Sandbox Code Playgroud)