I can't seem to get inheritance of a custom property to work. By that I mean,
when I decorate a property in a base class with my attribute, the attribute
is not applied to the derived class. A bare-bones example of my problem is
below. I have tried explicitly setting the Inherited property of the
attribute to true, even though this is supposed to be the default value.
// The attribute
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
public class HidePropertyAttribute : System.Attribute
{
}
// The base class
public abstract class BaseEntity
{
[HideProperty]
public virtual string SpouseName
{
get { return "Mary";}
}
}
// the derived class
public class Entity1 : BaseEntity
{
// property which should have attribute applied
public override string SpouseName
{
get
{
return "Betty";
}
}
public override string DogName
{
get
{
return "Muffy";
}
}
}
// Main method for console to test attributes
static void Main(string[] args)
{
Entity1 ent = new Entity1();
string output = string.Empty;
foreach(System.Reflection.PropertyInfo pi in
ent.GetType().GetProperties(System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.Public))
{
object[] hideAttribs =
pi.GetCustomAttributes(typeof(HidePropertyAttribute),true);
if (hideAttribs.Length == 0)
{
output += pi.Name + ": " + pi.GetValue(this, null) +
Environment.NewLine;
}
}
}
return output;
}
|