Attributes - can they know what they have been applied to?

Merrion

Junior Contributor
Joined
Sep 29, 2001
Messages
265
Location
Dublin, Ireland
I have a custom class that inherits from System.ComponentModel.CategoryAttribute and I want to override the GetLocalizedString function to return a localised category other than the standard ones (Appearance, Behaviour, Data etc.)

Is it possible for an attribute to get the type name of the thing it is applied to? The reason being that the resourcemanager that returns the description and category attribute needs to know which component type it is being called to return the localised string for.

Thanks in advance,
Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com
 
Well it doesn't look like the Attribute class has any protected
members or methods that would return that information. The
only two ways I can think of would be to

1.) pass the Type that the attribute is being applied to in its
constructor(s) (which is lame, I admit), or

2.) if the Type class has some sort of method that returns
what it's been applied to, use this.GetType() and figure it out
from there. I don't know if such a method exists, but if it does I'd
think it'd be in the Type class or somewhere in the Reflection
namespace.
 
I can do "LocalisedDescriptionAttribute" because you can set DescriptionAttribute value after creation - but strangely you cant do this with a CategoryAttribute.
 
Back
Top