PC Review


Reply
Thread Tools Rate Thread

Base class property Attribute not applied to derived class propert

 
 
=?Utf-8?B?RGF2aWQgTA==?=
Guest
Posts: n/a
 
      9th Mar 2005
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;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dotfuscator disassociates derived class when base class is linkedwith other assemblies Michael Letterle Microsoft Dot NET Compact Framework 0 22nd Apr 2008 09:59 PM
Base Class Method to use Shadow'ed member variable of Derived Class? Joe HM Microsoft VB .NET 2 4th Apr 2006 03:59 PM
Overhead base class versus derived class method invocation question Microsoft C# .NET 1 20th Dec 2005 02:29 PM
REPOST:Hiding base class property from derived class instance Baski Microsoft C# .NET 7 14th Dec 2004 10:04 PM
Want to take the path of specific overloaded constructors from derived class up to just one below the base class. hazz Microsoft C# .NET 3 15th Aug 2004 01:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:10 AM.