0

So I have a the following classes

class Person
{
    virtual string Property{get;set;}
}

and

class Manager : Person
{
    [SomeAttribute("Hello")]
    override string Property {get;set;}
}

If I have a member expression on type Manager, ie:

Property prop = PropertyGetter.GetProp<Manager>(p => p.Property)

Then the ReflectedType of the MemberExpression is Person, rather than Manager. This means that the Attribute information is lost. So:

var attribute = prop.GetAttribute<SomeAttribute>();

Then attribute is null.

I'm assuming this is because the property is from the base class, not defined in Manager, but how can I get around this?

Dave
  • 1,645
  • 2
  • 23
  • 39

1 Answers1

0

The issue is fixed if I use the new keyword on the property rather than virtual and override.

I think it has to do with the fact that there is no instance of the type Manager.

Another way to fix was to change my implementation of GetProp to do

return typeof(T).GetProperty(property.Name)

rather than just returning the property

Dave
  • 1,645
  • 2
  • 23
  • 39
  • You should consider the implications of shadowing though. Your shadowed property *must* proxy `base.Property` getter and setter. If you don't do this and you happen to pass a `Manager` to a method which accepts `Person` (or upcast `Manager` by any other means), you will potentially lose `Property` information. – Kirill Shlenskiy Jan 06 '16 at 23:49