简体   繁体   English

如何创建将作为参数传递的泛型类型的列表(VB.Net)

[英]How can I create a list of a generic type that will be passed as a parameter (VB.Net)

say I have two POCOs I'm using in EF code first 说我先在EF代码中使用两个POCO

Public class C1
    property F1 as integer
End Class

Public class C2
    property F2 as String
End Class

I want to have a function that will create a list either of C1 or C2, to be used in some generic operation, such that 我想拥有一个可以创建C1或C2列表的函数,以便在某些通用操作中使用,例如

Sub MySub(type_of_class)
    private lst as new List(of type_of_class)
    ...
End Sub

Will either create a list of C1 or of C2. 将创建C1或C2的列表。

I know generics are probably the answer by my knowlege is kind of generic :-) 我知道泛型可能是我的知识的答案是一种泛型:-)

thanks! 谢谢!

Generics is your answer, but there is a lot more to it. 泛型是您的答案,但还有很多其他问题。 Your sub should be: 您的订阅应该是:

Public Sub MySub(Of T)()

    Dim MyT As T

    'do stuff with T

End Sub

and called as this 并这样称呼

MySub(Of C1)()

For more (starting) info: msdn 有关更多(开始)信息: msdn

Success, Marcel 成功,马塞尔

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

相关问题 如何基于传递给VB.NET泛型方法的类型执行条件逻辑 - How do I execute conditional logic based upon the type passed to a VB.NET generic method 我可以使用什么作为List的通用,所以我可以在vb.net中更改列表中的类型 - what can i use as a generic of List so i can change the type within the list in vb.net 创建并将通用类型列表分配给VB.Net中的属性 - Create and Assign a List of Generic Type to a property in VB.Net 如何在vb.net中忽略值参数,如何确定一个值是否是泛型类型的实例? - How do I determine if a value is an instance of a generic type, ignoring the type parameter, in vb.net? 我可以在lamba表达式中包含泛型类型参数吗? (VB.NET 2010) - Can I include a Generic type parameter in a lamba expression? (VB.NET 2010) VB.NET通用列表类型过滤器:如何做? - VB.NET Generic list type filter: How to do it? 如何在VB.NET中创建通用属性? - How do I create a generic property in VB.NET? 我可以在VB.NET中限定参数的类型吗? - Can I qualify the type of a parameter in VB.NET? 如何将自定义类的常规列表绑定到datagridview并仅在VB.NET中显示特定属性? - How can i bind a generic List of custom classes to a datagridview and only show specific properties in VB.NET? 如何使用 vb.net 创建包含不同类型数据的列表? - How to create a list with different type of data using vb.net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM