合并两个PropertyInfo

时间:2016-06-08 14:48:58

标签: vb.net

我的命名空间Dimension中有两个结构和一个类。这些结构为Dimension.DerivedDimension.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?

1 个答案:

答案 0 :(得分:1)

And是一个布尔运算符。这是真/假值。您想从这两种类型中获取PropertyInfo的列表,请尝试:

Dim properties as List(Of PropertyInfo) = New List(Of PropertyInfo)
properties.AddRange(oType1.GetProperties())
properties.AddRange(oType2.GetProperties())