我的命名空间Dimension中有两个结构和一个类。这些结构为Dimension.Derived和Dimension.Basis。这门课叫Exponent。我重写了类的函数ToString(),以便在结构Dimension.Derived中获得属性的DisplayNameAttribute。
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-Operator没有为PropertyInfo声明。当然,对于另一个结构,我可以重复for -每个循环,但这不是目的。我该怎么做才能合并这些PropertyInfos?
发布于 2016-06-08 15:07:20
And是一个布尔运算符。是真值/假值。您希望从这两种类型中获得一个PropertyInfo列表,所以请尝试:
Dim properties as List(Of PropertyInfo) = New List(Of PropertyInfo)
properties.AddRange(oType1.GetProperties())
properties.AddRange(oType2.GetProperties())https://stackoverflow.com/questions/37705890
复制相似问题