对最高一级属性的思考
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
DeclaredOnly
GetProperties
调用中的绑定标志。这应该将返回的属性限制为指定的继承(类)级别。Try using the
DeclaredOnly
binding flag in yourGetProperties
call. This should limit the properties returned to the inheritance (class) level specified.