我想创建一个类,它将具有2个属性名称(作为类型字符串)和类型(作为任何类型)。我的想法是我可以创建一个我的类的集合,这样我就可以传递我的参数集
Public Class parameter
Dim m_ParameterName As String
Dim m_ParameterType As Type
Public Property ParameterName As String
Get
Return m_ParameterName
End Get
Set(ByVal value As String)
m_ParameterName = value
End Set
End Property
Public Property ParameterType() As T
Get
Return m_ParameterType
End Get
Set(ByVal value As T)
m_ParameterType = value
End Set
End Property
Sub New()
m_ParameterType = Nothing
m_ParameterName = ""
End Sub
End Class
然后我想创建一个类的实例并将属性定义为
Dim myParameter As New parameter
myParameter.ParameterName = "Name"
myParameter.ParameterType = String
or
Dim myParameter As New parameter
myParameter.ParameterName = "Name"
myParameter.ParameterType = integer
or
Dim myParameter As New parameter
myParameter.ParameterName = "Name"
myParameter.ParameterType = clsCustomClass
这可能吗?
答案 0 :(得分:2)
You may have to create a generic class to do this
您可以将课程定义为
Public Class SampleClass(Of T)
Private m_sampleProperty As T
Public Property SampleProperty() As T
Get
Return m_sampleProperty
End Get
Set
m_sampleProperty = value
End Set
End Property
End Class
用法:
Dim strClass As New SampleClass(Of String)()
strClass.SampleProperty = "wohoo_string"
Dim int32Class As SampleClass(Of Int32) = New SampleClass(Of Integer)()
int32Class.SampleProperty = 10
答案 1 :(得分:1)
1)您无法创建通用属性,因为编译器永远不会知道要使用哪个getter。 假设你有
Public Property SomeProperty() As T
Get
Console.WriteLine(GetType(T).Name)
Return Nothing
End Get
End Property
这会为Dim a = SomeProperty
打印什么?
2)ParameterType
应该有Type
类型。
3)您应该使用GetType()
运算符http://msdn.microsoft.com/en-US/library/tay4kywk.aspx
例如,parameter.Type = GetType(String)
答案 2 :(得分:0)
答案 3 :(得分:0)
Dim m_ParameterName As String
Dim m_ParameterType
Public Property ParameterType()
Get
Return m_ParameterType
End Get
Set(ByVal value )
m_ParameterType = value
End Set
End Property
只需将类型留空即可 - 就像c#
中的“var”一样另一种选择是使用As Object