我的命名空间Dimension
中有两个结构和一个类。这些结构为Dimension.Derived
和Dimension.Basis
。这个班叫做Exponent
。我已经覆盖了我班级的ToString()
函数,以便在我的结构中DisplayNameAttribute
获取Dimension.Derived
个属性。
Public Overrides Function ToString() As String
Dim oType As Type
oType = GetType(Dimension.Derived)
Dim colMemberInfo() As PropertyInfo = oType.GetProperties
For Each oMemberInfo In colMemberInfo
If Me = oMemberInfo.GetValue(oMemberInfo) Then
Dim de As New Dimension.Exponent
de = oMemberInfo.GetValue(oType)
Dim attr() As DisplayNameAttribute = DirectCast(oMemberInfo.GetCustomAttributes(GetType(DisplayNameAttribute), False), DisplayNameAttribute())
If attr.Length > 0 Then
Return attr(0).DisplayName
Else
Exit For
End If
End If
Next
Return Nothing
End Function
工作正常,但它应该搜索两个结构。因此,我将第一行改为
Dim oType1, oType2 As Type
oType1 = GetType(Dimension.Derived)
oType2 = GetType(Dimension.Basis)
Dim colMemberInfo() As PropertyInfo = oType1.GetProperties And oType2.GetProperties
但是这引发了一个异常,即And
- 运算符未声明为PropertyInfo
。当然,我可以为另一个结构重复For-Each-loop,但这不是意图。如何合并这些PropertyInfos?
答案 0 :(得分:1)
And
是一个布尔运算符。这是真/假值。您想从这两种类型中获取PropertyInfo
的列表,请尝试:
Dim properties as List(Of PropertyInfo) = New List(Of PropertyInfo)
properties.AddRange(oType1.GetProperties())
properties.AddRange(oType2.GetProperties())