|Deep C# - Custom Attributes In C#|
|Written by Mike James|
|Friday, 19 March 2021|
Page 3 of 6
You might think that as you can pass an object you can pass anything but this isn’t the case. There is an additional condition that restricts parameters to constants of the appropriate type.
So you can have:
but you can’t have:
or anything like it.
Each time you request the details of an attribute a new object is instantiated no matter what class is involved.
So for example, if we go back to the simpler version of ASCII with a single parameter constructor, the following:
does indeed create distinct attribute instances as you can demonstrate:
The result is two messageboxes that correctly show each of the messages set by the attribute tags.
However you get two instances even if you call GetCustomAttributes a second time on the same class.
What isn’t generally realised is that you can change attribute values fairly easily at runtime. The reason is, of course, that the instances of the attribute classes that are created are perfectly normal objects and can be used without restriction.
For example, we can get the object:
change the value of its public variable and show that it has changed:
and finally create another instance and show that its value is unchanged:
Now that we have the basic mechanism that is used to implement attributes out in the open and perfectly clear we need to investigate some of the intricacies of using attributes in a real situation.
The first thing to say is that you can apply attributes to more than just a class. To restrict what an attribute can be applied to you need to apply the AttributeUsage attribute to the class that is associated with your custom attribute - yes an attribute applied to your custom attribute!
restricts the ASCII attribute to being applied to methods only. If you try to apply it to anything else the compiler will generate an error message.
IntelliSense provides a list of AttributeTargets and you can OR targets together to produce a set of things that the attribute can be applied to - an example of this is given later.
Being able to tag other entities with attributes raises the question of how to get at them at runtime?
The answer is that there a number of overloaded versions of GetCustomAttributes which return arrays of attributes associated with different types of entity.
For example, GetCustomAttributes(memberinfo) will retrieve an array of attributes applied to the member specified by the memberinfo class.
To see how this works first add a member function to MyClass and tag it with the ASCII attribute:
To retrieve attributes associated with a member we first have to retrieve its memberinfo. There are many ways of doing this but in most cases specifying it by name is sufficient using the GetMember function:
The GetMember function retrieves all members that match the name specified string which can include wildcard characters.
In this case it should just return a memberinfo class corresponding to the unique MyMethod function. To retrieve any attributes applied to the method we can use the usual:
Or if you just want the ASCII attributes that have been applied we can use the alternative:
The GetMember function will return members of a range of types – methods, properties etc.
|Last Updated ( Friday, 19 March 2021 )|