problem using custom attributes

fguihen

Junior Contributor
Joined
Nov 10, 2003
Messages
248
Location
Eire
i decared a custom attribute class:
C#:
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Field|AttributeTargets.All, AllowMultiple = false)]
    public class AssemblyTypeName : System.Attribute
    {
       
        string itemName;
        public string NameOfClass
        {
            get
            {
                return itemName;
            }
        }
        public AssemblyTypeName(string nameOfClass)
        {
            itemName = nameOfClass;
        }
    }


now, just above the class that i want to use the custom attribute i place the line:
C#:
    [ AssemblyTypeName("BusinessEmployee")]

i read in the assembly data for the assembly that uses the custom attribute,but i cant find out how to access my custom attribute ( before i instantiate the class).it is being used to identify the class in order to see if its the one i want to create an instance of.
 
Last edited by a moderator:
You probably want to use reflection. Maybe load the assembly for reflection, find the type(s) you want to examine, and then use the Attribute.GetCustomAttribute function to load the attributes. The attribute type will need to be loaded (and not just for reflection) though in order for this to work.

I've never actually done this before so there may be something I've missed.
 
Back
Top