在VB.NET上反映派生类的唯一子属性
我试图使用反射序列化从另一个继承的类的属性,但我只想序列化子类的属性,而不是父类的属性。我该怎么做 这就是我正在做的,不幸的是,它也得到了父类的所有属性,正如人们所期望的:在VB.NET上反映派生类的唯一子属性,vb.net,oop,reflection,Vb.net,Oop,Reflection,我试图使用反射序列化从另一个继承的类的属性,但我只想序列化子类的属性,而不是父类的属性。我该怎么做 这就是我正在做的,不幸的是,它也得到了父类的所有属性,正如人们所期望的: Public Function SaveData() As System.Collections.Generic.List(Of System.Xml.Linq.XElement) Implements Interfaces.ICustomXMLSerialization.SaveData Dim
Public Function SaveData() As System.Collections.Generic.List(Of System.Xml.Linq.XElement) Implements Interfaces.ICustomXMLSerialization.SaveData
Dim elements As New List(Of System.Xml.Linq.XElement)
Dim ci As CultureInfo = CultureInfo.InvariantCulture
With elements
Dim props As PropertyInfo() = Me.GetType.GetProperties()
For Each prop As PropertyInfo In props
If TypeOf Me.GetType.GetProperty(prop.Name).PropertyType Is IList Then
.Add(New XElement(prop.Name, DWSIM.App.ArrayToString(Me.GetType.GetProperty(prop.Name).GetValue(Me, Nothing), ci)))
ElseIf TypeOf Me.GetType.GetProperty(prop.Name).GetValue(Me, Nothing) Is Double Then
.Add(New XElement(prop.Name, Double.Parse(Me.GetType.GetProperty(prop.Name).GetValue(Me, Nothing)).ToString(ci)))
Else
.Add(New XElement(prop.Name, Me.GetType.GetProperty(prop.Name).GetValue(Me, Nothing)))
End If
Next
End With
Return elements
End Function
提前感谢,,
Daniel您需要指定
BindingFlags.DeclaredOnly
作为GetProperties
调用的参数
不过,这些标志的使用经常会遇到一些问题,因此可能需要进行一些实验才能找到所需的标志的精确组合。枚举的MSDN描述为。根据定义,父对象的属性也是子对象的属性!