gpt4 book ai didi

vb.net - 如何确定泛型类 VB.NET 中的类型

转载 作者:行者123 更新时间:2023-12-01 12:37:16 25 4
gpt4 key购买 nike

假设我有这样一个类:

Public Class(Of T As Bar) Foo
...
End Class

我想设置如下内容:

Public Class(Of T As Bar) Foo
Public Sub New()
Select T
Case Class1 'Inherits Bar
'do stuff
Case Class2 'Inherits Bar
'do stuff
Case Class3 'Inherits Bar
'do stuff
End Select
End Sub
End Class

显然这行不通,但您或许可以从这个片段中了解我正在尝试做什么。那么如何在 VB.NET 中正确地确定传递给构造函数的类型呢?令人惊讶的是,我在 Stack Overflow 上找不到任何关于此的内容。

C# 的语法如下:

Type typeParameterType = typeof(T);

但是,

Dim typeParameterType As Type = TypeOf T

不行,也不行

Dim typeParameterType As Type = T

最佳答案

C# 不允许在 typeof 上使用 switch。但是,VB 允许这样做,因此您可以尝试这样的操作:

Public Class Foo(Of T As Bar)
Public Sub New()
Select Case GetType(T)
Case GetType(BarA)
' Do stuff

Case GetType(BarB)
' Do stuff
End Select
End Sub
End Class

关于vb.net - 如何确定泛型类 VB.NET 中的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28587560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com