Wednesday, 14 March 2018

047 C# Attribute

Attribute is a special type of tag top of an element to convey some information to run time.Attribute add meta data to the  program of element. .Net by default content meta data in the Assembly. You can add additional metadata with the help of attribute. The declarative tag attribute is written at the top of the element within opening and closing braket([..]) . Comments , description can also be added with attributes. Attribute are two to type
  1. Predefined 
  2. Customs

. Net Framework have Predefined attributes 
  1. AttributeUsage 
  2. Conditional 
  3. Obsolete





AttributeUsage
 
AttributeUsage : The AttributeUsage, specify the type on whom attribute will be applicable. List of type can be added with (|) operator or all can we mention if you want to apply all element. AttributeUsage comes under System.AttributeUsage class.AttributeUsage can be apply on class, structure ,Method,Assembly,Event ect.

AllowMultiple : This property tell the computer compiler that attribute is used for multi use . AllowMultiple received Boolean value true or false. The default is false , that means the multi property is false if not specified that is for single use.


Inherited : Inherited is a attribute which is used to tell compiler that attribute will be available during the inheritance. If a parent is declared with the attribute child will get this attribute or not. default property is false.






using System;

[AttributeUsage(AttributeTargets.Class | 
 AttributeTargets.Struct |  
 AttributeTargets.Method, 
 AllowMultiple = true
 Inherited = false)]

public class MyStudentClass : Attribute
{
      static void Main(string[] args)
      {
          Console.ReadKey();
       }
}



Conditional: Conditional attribute is used very rare really. It use processor directives define or not. Processor directories define at the top of the program, with symbol #. Processor definition determine the execution of conditional  attribute.

#define LEARN
#undef PROGRAM

using System;
using System.Diagnostics;

class Program
{
       static void Main()
      {
       Example1();
       Example2();
     }

     [Conditional("LEARN")]
     static void Example1()
    {
          Console.WriteLine("LEARN is defined");
          Console.ReadKey();
    }

    [Conditional("PROGRAM")]
     static void Example2()
     {
       Console.WriteLine("PROGRAM is defined");
       Console.ReadKey();
    }
}

Output :
LEARN is defined


Obsolete:Obsolete attribute used to generate compile time warning.When up gradation or version changed , software method become unusable , then a attribute is added to the method. It prevent an obsolete method to call and execute. An information message also can be attached with the absolute attribute which inform the new method name to inform the developer. The first parameter of obsolete attribute is message and the second parameter iserror. If you iserror  as true. The compiler will generate an error.

using System;

class MyExampleClass
{
          [Obsolete("Method Example1 is no more is Use, Use method Example2", true)]
          public void Example1()
          {
                 Console.WriteLine("Execute Example1");
          }

          public void Example2()
         {
                 Console.WriteLine("Execute Example2");
         }
}

public class Programme
{

         static void Main(string[] args)
        {
              MyExampleClass obj = new MyExampleClass();
              obj.Example1();
              obj.Example2();
              Console.ReadKey();
         }
}


//Error
'MyExampleClass.Example1()' is obsolete: 'Method Example1 is no more is Use, Use method Example2'



Creating Custom Attributes
You can create your own custom attribute. You need to declare the class first, you need to inherit system.attribute. This class can be apply to target class.

using System;
[System.AttributeUsage(System.AttributeTargets.Class |
System.AttributeTargets.Struct)
]

public class MyAttribute : System.Attribute
{
       private string Param1;
       private double Param2;

      public MyAttribute(string Verion_Name, double Verion_Version)
      {
         this.Param1 = Verion_Name;
         this.Param2 = Verion_Version;
     }
}


[MyAttribute("C# Tutorial",1.5)]
class MyClass1
{
}

[MyAttribute("C# Tutorial 2010", 2.0)],
[MyAttribute("C# Tutorial 2011", 2.5)]
class MyClass2
{

}

How to read the Attribute of a class : Attribute is of a class can be read with the help of reflection. Reflection help you get the typeinfo of the class.Once typeinfo  is available,attribute can be read. There are two type of method to get the  attribute
GetCustom attribute
Obsolete attribute
/*example pending**************/

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...