How to create custom attributes in .Net

Attribute:

An attribute is a piece of additional declarative information that is specified for a declaration.

 Attributes are needed to write codes in a better style, in a better way. Its may not be mandatory to use attributes in your coding but it should be used for a better architecture. It can be used to define both design-level information and run-time information. It can also be used to create self-describing components using attribute. .Net framework ships with several default attributes like [Serializable()] , [Obsolete()] etc. But what if you want to create your own attribute i.e custom attribute. Please find a simple example  below.

Code for creating custom attribute

namespace ConsoleApplication1

{

    public class GreetWithFullName : Attribute

    {

        private bool bVal = false;

 

        public bool GetValue

        {

            get { return bVal; }

        }

 

        public GreetWithFullName(bool val)

        {

            bVal = val;

        }

    }

}
Every custom attribute should be inherited from System. Attribute class [as shown above].
How to use your custom attribute
namespace ConsoleApplication1

{

    [GreetWithFullName(true)]

    public class MFSEmployee

    {

        private string _firstName, _lastName;

         public MFSEmployee(string firstName, string lastName)

        {

            this._firstName = firstName;

            this._lastName = lastName;

        }

 

        public string SayHello()

        { 

            string greetWith = string.Empty;

            Type type = this.GetType();

             GreetWithFullName[] AttributeArray =

               (GreetWithFullName[])type.GetCustomAttributes(typeof(GreetWithFullName),false);

 

            if ((AttributeArray.Length > 0) && (AttributeArray[0].GetValue == true))

            {

                greetWith = "Hello " + this._firstName + " " + this._lastName;

            }

            else

            {

                greetWith = "Hello " + this._firstName;

            }

            return greetWith;

        }

    }

}

Note the class that marked with [GreetWithFullName(true)]. Now when the SayHello() will be called, it will first check whether the GreetWithFullName is there and set to true for the calling class. If so, then it returns a string with full name (i.e first name followed by last name). Otherwise it simply returns the first name.

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            MFSEmployee emp = new MFSEmployee("Priyanka", "Dash");

            Console.WriteLine(emp.SayHello());

            Console.ReadLine();

        }

    }

}
Output I [With GreetWithFullName(true)]

Hello Priyanka Dash

Output II [With GreetWithFullName(false) or not present at all]

Hello Priyanka

This is a simple example to understand creating Custom Attribute. You can create and use Custom Attributes in your projects to get excellent run-time behavioral characteristics.

150 150 Burnignorance | Where Minds Meet And Sparks Fly!