对最高一级属性的思考

发布于 2024-09-24 09:25:59 字数 397 浏览 2 评论 0原文

class CBase
{
 object A {get;set;}
 object B {get;set;}
}

class CDerived : CBase
{
 object X {get;set}
 object Y {get;set;}
}

我正在尝试获得一级属性。对于上面的示例,预期属性是 X 和 Y,而不是 A 和 B。通过以下代码,我将获得所有属性 {A,B,X,Y}。有没有没有属性签名的解决方案。

foreach (var propertyInfo in typeof(CDerived).GetProperties())
{
 propertyInfo.SetValue(model, row[propertyInfo.Name], null);
}
class CBase
{
 object A {get;set;}
 object B {get;set;}
}

class CDerived : CBase
{
 object X {get;set}
 object Y {get;set;}
}

I'm trying to get first level properties. For the example above, intended properties are X and Y, not A and B. With the following code i'm getting all the properties {A,B,X,Y}. Is there any solution without attribute signing.

foreach (var propertyInfo in typeof(CDerived).GetProperties())
{
 propertyInfo.SetValue(model, row[propertyInfo.Name], null);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

银河中√捞星星 2024-10-01 09:25:59

尝试使用 DeclaredOnly GetProperties 调用中的绑定标志。这应该将返回的属性限制为指定的继承(类)级别。

Try using the DeclaredOnly binding flag in your GetProperties call. This should limit the properties returned to the inheritance (class) level specified.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文