Attribute in c#


An attribute is mainly derived from the library – “System.Diagnostics” but all the attributes were inherited from the Sealed class called Attribute this attribute class were basically implemented with the help of “System.Relfection and System.Runtime.InteropServices”. this will help to avoid writing complicated logic to validate the basic details and to restrict the operation to be performed with the properties/class/Methods.
What is Attribute in c#?
Attribute is used to provide a details information about a property or class or a method and even for structure behavior as well during the run time of an application execution (even for a console application).
An Attribute is defined above a class or method or a property will define the functionality of that element. A attribute is defined by square ([ ]) Brace used above the required element.
The .Net Framework supports 2 attributes types:
1.      Pre-defined
2.      custom built
Syntax:

Here,
·        Attribute Usage à Attribute Name (One of Predefined Attribute)
·        Parameter Such As,
o   AttributeTargets à To which element it must validate.
o   AllowMultiple à To Define Decorated attribute is multi Use or not (Boolean Property).
o   Inherited à To Defined Decorated attribute is derived by Child Class or Not (Boolean Property).
Predefined:
There are 3 pre-defined attributes available and supported by .Net.
1.      AttributeUsage
2.      Conditional
3.      Obsolete
AttributeUsage:
AttributeUsage mainly describes how to customize a class with the help of attribute. If attribute is mentioned with an optional parameter Inherited as false, then the attribute specified class cannot be inherited by any the other classes.
 In Other words, if a class or method or property is addressed or decorated with the AttributeUsage (Inherited = false) then that element cannot be accessed in derived class .

Example,
[AttributeUsage (AttributeTargets.All, AllowMultiple = true, Inherited =false)]
Conditional:
Defines conditional method whose execution depends on a mentioned preprocessing identifier.
1.      Debug
2.      Trace.
Syntax:
[Conditional(“Preprocessing identifier”)]
Example,
#define DEBUG
using System;
using System.Diagnostics;
public class DemoClass {
   [Conditional("DEBUG")]
      public static void DisplayData(string value) {
      Console.WriteLine(value);
   }
}
class validate {
   static void CallerMethod1() {
      DemoClass.DisplayData ("Im Calling from CallerMethod1.");
      CallerMethod2();
   }
   static void CallerMethod2() {      DemoClass.DisplayData ("Im Calling from CallerMethod2");   }
   public static void Main() {
      DemoClass.DisplayData ("Execution Starts.");  CallerMethod1(); Console.WriteLine(“Execution Ends”); Console.ReadLine();
   }
}
Obsolete:
This attribute defines that an element that has been decorated with this attribute should be used for any of the implementation or if an user tries to use an obsoleted method then complier will throw an warning that so and so method was defined as obsoleted so please use the latest method.
Syntax,
[Obsolete (Message,Iserror)]
Here,
1.      Message à“Reason for the element has been obsoleted”,
2.      IsError à Define whether any error must be thrown, if obsoleted method has been consumed
Example,
using System;
public class DemoClass {
   [Obsolete("Im Obsoleted, So don’t use me , go for latest", true)]
      static void ObsoletedMethod() {
      Console.WriteLine("I’m Obsoleted method");
   }
   static void LatestVersionMethod() {
      Console.WriteLine("I’m LatestVersionMethod ");
   }
   public static void Main() {
      ObsoletedMethod ();
   }
}
Error thrown è Im Obsoleted, so don’t use me, go for latest

Comments

Popular posts from this blog