Friday, July 1, 2011

C#.net - Custom Attributes in C#.net

An attribute is an object that represents data you want to associate with an element in your program. The element to which you attach an attribute is referred to as the target of that attribute.
Attributes are special classes that can be applied to classes, properties, and methods at design time. Attributes provide a way to describe certain aspects of an elementor determine the behavior of other classes acting upon the element. Those descriptions and behaviors can then be accessed and examined at runtime. You can think of attributes as a way of adding special modifiers to your class members.


Let See how to create a Custom attribute in C#.net.


Step 1
Create a Console Application and give the solution name as  ConCustomAttribute.


Step 2
Declaring an Attribute,it is look like this
 [AttributeUsage(AttributeTargets.All,AllowMultiple=true)]
    public class DeveloperAttribute:System.Attribute
    {
    }

To create a custom attribute, derive your new custom attribute class from System.Attribute.
AttributeUsage is an attribute applied to attributes: a meta-attribute. It provides, if you will, meta-metadata--that is, data about the metadata. For the AttributeUsage attribute constructor, you pass two arguments. The first argument is a set of flags that indicate the target--in this case, the class and its constructor, fields, methods, and properties. The second argument is a flag that indicates whether a given element might receive more than one such attribute. In this example, AllowMultiple is set to true, indicating that class members can have more than one attributes assigned.


Step 3
Declaring Constructor and Properties,it is look like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConCustomAttribute
{
    [AttributeUsage(AttributeTargets.All,AllowMultiple=true)]
    public class DeveloperAttribute:System.Attribute
    {
        #region Enum

        /// 
        ///  User Defined Developer Name
        /// 
        public enum DeveloperName
        {
            BhushanPawar = 0,
            RamdasBhosale = 1,
            KosteBudinoski = 2,
            KishorNaik = 3
        };

        #endregion

        #region Constructor
        
        /// 
        ///  Blank Constructor
        /// 
        public DeveloperAttribute()
        {

        }

        /// 
        ///  Write a Attribute 
        /// 
        /// 






Specify Developer Name
        /// 






Specify the Date
        /// 






Specify the Comment
        public DeveloperAttribute(DeveloperName Developer,String Date,String Comment)
        {
            try
            {
                this.Developer = Developer;
                this.Date = Date;
                this.Comment = Comment;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);  
            }
        }

        #endregion

        #region Property
        /// 
        ///  Get and Set the Developer Name
        /// 
        public DeveloperName Developer
        {
            get;
            set;
        }

        /// 
        ///  Get and Set the Date
        /// 
        public String Date
        {
            get;
            set;
        }

        /// 
        ///  Get and Set the Comment   
        /// 
        public String Comment
        {
            get;
            set;
        }

        #endregion
    }

    
}

Declaring Constructor
Attributes are initialized with constructors in the same way as traditional classes.Every attribute must have at least one constructor.


Declaring Properties
If we want to define a named parameter or provide an easy way to return the values stored by your attribute, declare a property. Attribute properties should be declared as public entities with a description of the data type that will be returned. Define the variable that will hold the value of your property and associate it with the get and set methods.


Step 4
Using an Attribute in your class,it is look like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConCustomAttribute
{
    [Developer(DeveloperAttribute.DeveloperName.KishorNaik,"1/6/2011","Class Created by System")]
    public class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Method1();
                Method2();
                Method3();
                Method4();
                Method5();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message); 
            }
        }

        #region Methods

        [Developer(DeveloperAttribute.DeveloperName.KishorNaik,"1/6/2011","Complete")]
        public static void Method1()
        {
            System.Console.WriteLine("Kishor naik Code"); 
        }

        [Developer(DeveloperAttribute.DeveloperName.RamdasBhosale, "1/6/2011", "Complete")]
        public static void Method2()
        {
            System.Console.WriteLine("Ramdas Bhosale Code");
        }

        [Developer(DeveloperAttribute.DeveloperName.KosteBudinoski, "1/6/2011", "Complete")]
        public static void Method3()
        {
            System.Console.WriteLine("Koste Budinoski Code");
        }

        [Developer(DeveloperAttribute.DeveloperName.BhushanPawar, "1/6/2011", "Complete")]
        public static void Method4()
        {
            System.Console.WriteLine("Bhushan Pawar Code");
        }

        [Developer(DeveloperAttribute.DeveloperName.KishorNaik, "1/6/2011", "I Passed below function to Koste Budinoski")]
        [Developer(DeveloperAttribute.DeveloperName.KosteBudinoski,"1/6/2011","I Received this function from kishor naik and i completed")]
        public static void Method5()
        {
            System.Console.WriteLine("kishor Naik Code");
            System.Console.WriteLine("Koste Budinoski Code");
        }

        #endregion
    }
}

Note
The new custom attribute in this example is named DeveloperAttribute. The convention is to append the word Attribute to your attribute name. The compiler supports this by allowing you to call the attribute with the shorter version of the name. Thus, you can write:
 [Developer(DeveloperAttribute.DeveloperName.KishorNaik,"1/6/2011","Complete")]

The compiler will first look for an attribute named Developer and, if it does not find that, will then look for DeveloperAttribute.


Download
Download Source Code

No comments:

Post a Comment