mercredi 12 juillet 2017

Can I use reflection to get the base property that has been overridden via the new keyword?

I'm working with some code which uses custom attributes for validation, like this :

 public class BaseClass
 {
     [Min3CharsIfPopulated]
     public string Id { get; set; }
 }

I'm looking to create a child class with the same property name for binding purposes, but without the validation attribute.

 public class ChildClass: BaseClass
 {
     public new string Id { get; set; }
 }

The validation code using reflection to obtain the property value to validate

private static object GetValue(PropertyInfo p, object value)
{
    return p.GetValue(value, new object[0]);
}

however calling

GetValue(baseClassPropertyInfo, childClassObj);

is returning ChildClass.Id to validate using the attribute that was found on BaseClass.Id

Is it possible to use reflection to get BaseClass.Id rather than ChildClass.Id when looking at an object of type ChildClass?





Aucun commentaire:

Enregistrer un commentaire