
How to Define Custom Attributes in C# (C Sharp)If you want to associate any additional information with the programming objects like class or structures, you can store them as attributes. These attributes will be processed by the compiler and stored in metadata.Attributes can be classified into Pre-defined Attributes and Custom Attributes.
Pre-defined
attributes are used to store external information into metadata. For example,
consider the following piece of code: public class
testAttribute { In certain
cases, you might want to create and use your own attribute. You can very
well do it in C# by defining custom attributes. To create custom attribute,
follow the guidelines given below: Your
attribute can be visible to all programming objects or visible and accessible
only to specific programming objects. You can specify this visibility
by using AttributeUsage attribute which should be specified before defining
your attribute. If you want your attribute to be visible to all programming
objects, use the following line of code: [System.AttributeUsage(System.AttributeTargets.All)] If your
attribute has to be visible only to classes then specify the following
code: [System.AttributeUsage(System.AttributeTargets.Class] If your
attribute has to be visible to both structures and classes but not to
any other programming objects then specify the following code: [System.AttributeUsage(System.AttributeTargets.Struct
|System.AttributeTargets.Class)] You
have to define a new class for creating custom attribute. o Positional:
These are mandatory parameters. They have to be supplied when your custom
attribute is being used. Public constructor of your attribute class should
accept these mandatory parameters as input. o Named:
Named Parameters are optional. You can pass them when using the attribute
or you can leave them unspecified. Once
you have created attribute class with parameters defined, you can use
the attribute wherever appropriate. When using or specifying value for
the attribute, you have option to specify one value for the attribute
or multiple values for the same attribute. By default, you can specify
one value for the attribute. If you want to specify multiple values for
the same attribute, set AllowMultiple to true in AttributeUsage as shown
below: [System.AttributeUsage(System.AttributeTargets.All,
AllowMultiple = true)] Once
the attribute value is set, you can retrieve it using reflection mechanism
at run time. Given below
are code samples for custom attribute creation and usage: Code for
Attribute Class Creation: Using System; [AttributeUsage(AttributeTargets.All,
AllowMultiple = true)] public class
sampleCustomAttribute : System.Attribute { public sampleCustomAttribute(string
samplePosParam) { In this example
you have created a custom attribute called sampleCustomAttribute derived
from System.Attribute. This attribute class is accessible to all programming
objects and multiple values can be given to the attribute. This definition
is done using AttributeUsage. This class has positional parameter called
samplePosParam that is passed in the constructor. It also has a named
parameter called sampleNamedParam. Code for
Custom Attribute Usage: In this cocde
sample you are using sampleCustomAttribute and defining values to it.
Since it can accept multiple values, two different values are supplied
to the attribute, first line containing values for both positional and
named parameter. Second line has supplied only the positional parameter
value. Both are legal because named parameter is optional. Note that positional
parameter value is directly mentioned whereas named parameter value is
mentioned as: Both the
name of the named parameter and its value has to be mentioned. Code for
Retrieving Custom Attribute Value: class AttrRetrivalClass
{
Tweet
_______________________________________________________________________ _______________________________________________________________________
FREE
Subscription
Subscribe to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________ |