Reflection on Properties for highest level one [duplicate]
-
04-10-2019 - |
Question
This question already has an answer here:
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);
}
Solution
Try using the DeclaredOnly
binding flag in your GetProperties
call. This should limit the properties returned to the inheritance (class) level specified.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow