Reflecting only child properties of a derived class on VB.NET

I am trying to serialize the properties of a class that inherits from another using Reflection, but I would like to serialize only the properties of the child class, not the parent ones. How can i do this?

This is what I do, and unfortunately, it gets all the properties of the parent class, as you would expect:

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 

Thanks in advance, Daniel

+4
source share
1 answer

You need to specify BindingFlags.DeclaredOnly as a parameter for your GetProperties call.

However, these flags are often used, so you may need some experimentation to find the exact combination of flags. The MSDN listing description is here .

+5
source

Source: https://habr.com/ru/post/1441812/


All Articles