I have a Person
class that inherits EntityBase
:
public class Person : EntityBase
{
virtual public string FirstName { get; set; }
virtual public string LastName { get; set; }
virtual public IList<Asset> Assets { get; set; }
}
And
public class EntityBase : IEntity
{
public virtual long Id { get; protected set; }
public virtual string Error { get; protected set; }
}
I need to get list of properties of self Person
class:
var entity = preUpdateEvent.Entity;
foreach (var item in entity.GetType().GetProperties()) //only FirstName & LastName
{
if (item.PropertyType == typeof(String))
item.SetValue(entity, "XXXXX" ,null);
}
Now GetProperties()
is include : FirstName, LastName, Id, Error
but I need only own Person properties namely : FirstName, LastName
How can I get the properties that are only defined on Person
?
Use
var properties = typeof(Person).GetProperties(BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.DeclaredOnly);
The DeclaredOnly
value is documented like this:
Specifies that only members declared at the level of the supplied type's hierarchy should be considered. Inherited members are not considered.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With