VB.NET中如何仅显示派生类的哪些子属性?

2026-05-06 11:451阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计350个文字,预计阅读时间需要2分钟。

VB.NET中如何仅显示派生类的哪些子属性?

我正在尝试使用Reflection序列化从另一个继承的类中的属性,但只想序列化子类的属性,而不是父类的属性。我该如何做到这一点?这是我正在处理的情况,不幸的是,它正在获取父类的所有属性。

我正在尝试使用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

提前致谢,
丹尼尔

您需要将BindingFlags.DeclaredOnly指定为GetProperties调用的参数.

但是,经常会遇到使用这些标志的问题,因此可能需要进行一些实验才能找到所需标记的确切组合.枚举的MSDN描述是here.

VB.NET中如何仅显示派生类的哪些子属性?
标签:属性

本文共计350个文字,预计阅读时间需要2分钟。

VB.NET中如何仅显示派生类的哪些子属性?

我正在尝试使用Reflection序列化从另一个继承的类中的属性,但只想序列化子类的属性,而不是父类的属性。我该如何做到这一点?这是我正在处理的情况,不幸的是,它正在获取父类的所有属性。

我正在尝试使用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

提前致谢,
丹尼尔

您需要将BindingFlags.DeclaredOnly指定为GetProperties调用的参数.

但是,经常会遇到使用这些标志的问题,因此可能需要进行一些实验才能找到所需标记的确切组合.枚举的MSDN描述是here.

VB.NET中如何仅显示派生类的哪些子属性?
标签:属性