4

Reflection を使用して別のクラスから継承するクラスのプロパティをシリアル化しようとしていますが、親クラスではなく、子クラスのプロパティのみをシリアル化したいです。どうやってやるの?

これは私がやっていることですが、残念ながら、予想どおり、親クラスのすべてのプロパティも取得しています:

    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

前もって感謝します、ダニエル

4

1 に答える 1

6

BindingFlags.DeclaredOnly呼び出しのパラメーターとして指定する必要がありますGetProperties

ただし、これらのフラグの使用には問題があることが多いため、必要なフラグの正確な組み合わせを見つけるには、ある程度の試行錯誤が必要になる場合があります。列挙型の MSDN の説明はこちらです。

于 2012-10-24T13:23:42.280 に答える