Online courses in ASP.NET MVC, ASP.NET Core, and Design Patterns conducted by Bipin Joshi. Read more...
Learn ASP.NET MVC, ASP.NET Core, and Design Patterns through our online training programs. Courses conducted by Bipin Joshi on weekends. Read more details here.

<%@ Page %>

Introduction to attributes

Introduction

As you might be aware that .NET assemblies are self describing i.e. the information about each assembly is stored in the assembly itself. This information is called as Metadata. .NET allows you to put your own information in the metadata. The way to accomplish this is through Attributes. In this article we will see how to create your own attributes and use them in your code.

.NET itself uses attributes at many places. For example to declare your component as transactional you can use special attributes. The attributes can be used for variety of other purpose like specifying author information, copy right information, version information etc. Attributes are special classes that are used to 'annotate' your assemblies, classes or class members.

Using Attributes

Before going into the details of how to create an attribute let us see how you use attributes. Following example illustrates using attribute in C#. Don't worry if you can not understand the code completely we will be dissecting it in later sections.

using System;


[AuthorInfo(Name="bipin")]


public class AttbUsageDemo
{
public void Show()
{
Console.WriteLine("Using attributes...");
}
��

In the above code fragment the line marked in bold is of our interest. The line uses an attribute class whose name is AuthorInfo. The AuthorInfo class has some properties out of which one is Name. VB programmers will find the calling convention similar to named procedure parameters. In C# the attributes are enclosed between [ and ]. In VB the same declaration becomes as follows :

<AuthorInfo(Name="bipin")> public class AttbUsageDemo

Attributes can be applied to assemblies, classes, class members, structures etc. The member to which an attributed is applied is called as attribute target. If you are developing your own attributes you can control the attribute targets. For example you can design your attributes such that they can be used only against classes and not against class members or assemblies.

Creating custom attributes

All of your custom attribute classes must derive from System.Attribute class. In this section we will create an attribute called AuthorInfo. The class will have two properties Name and WebSite. The class will also have a constructor that takes a parameter for language. The entire class is given below.

using System;

[AttributeUsage(AttributeTargets.All)]
public class AuthorInfo:Attribute
{
string m_name;
string m_language;
public AuthorInfo(string language)
{
m_language=language;
}
public string Name
{
get
{return m_name;}
set
{m_name=value;}
}
public string Language
{
get
{return m_language;}
set
{m_language=value;}
}

}

Note how we have specified the attribute targets using AttributeUsage attribute. The AttributeTargets enumeration contains various possible targets like Class, Method, Property etc. Here, we have marked all the members as target. This simply means that we can use our attribute against our class, its methods and properties.

Now that our attribute class is ready, compile it into a DLL using following command :

csc /t:library authorinfo.cs

Using our attributes

In this section we will create another class that is marked with attributes we just created.

using System;
[AuthorInfo("C#",Name="bipin")]
public class AttbUsage
{
public void Show()
{
Console.WriteLine("This class uses
AuthorInfo attribute !");
}
public static void Main()
{
AttbUsage a=new AttbUsage();
a.Show();
}
}

Here, we have applied AuthorInfo attribute to the AttbUsage class. We have passed "C#" as the constructor parameter and name property.

Obtaining custom attribute information via reflection

In this final section we will obtain information about our AuthorInfo attribute via .NET reflection.

Note: If you are not familiar with reflection please refer my article Introduction to .NET reflection.

The following code extracts information about custom attributes applied to AttbUsage class. The Attribute class has a method called GetCustomAttribute() that returns an array of Attribute objects for the supplied class.

using System;
public class AttbReflection
{
public static void Main()
{
AttbUsage myattb=new AttbUsage();
Type t=myattb.GetType();
Attribute[] attb=
Attribute.GetCustomAttributes(t);
AuthorInfo ai=(AuthorInfo)attb[0];
Console.WriteLine("Author of the code is :"
+ ai.Name);
}
}

Summary

Attributes are powerful way to emit your own values in metadata. Attributes are special classes that are derived from System.Attribute class. Reflection APIs can be used to find information about custom attributes of an assembly.




Bipin Joshi is a software consultant, trainer, author and a yogi having 21+ years of experience in software development. He conducts online courses in ASP.NET MVC / Core, jQuery, AngularJS, and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced Yoga way of life he also teaches Ajapa Meditation to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 22 Jul 2001



Tags : .NET Framework VB.NET C# Components Programming Languages