1. C# - Attributes:
Attributes allow you to add declarative information to your programs. This information can then be queried at runtime using reflection. Atrributes can be applied on class, methods, assemblies etc. You You can add declarative information to a program by using an attribute. A declarative tag is depicted by square ([ ]) brackets placed above the element it is used for.
A few pre-defined attributes with in the .NET framework.
Predefined Types Of Atrributes
1). AttributeUsage
2). Conditional
3). Obsolete
1). AttributeUsage:
It specifies the types of items to which the attribute can be applied.
Syntax for specifying this attribute is as follows:
[AttributeUsage(
validon, //The parameter validon specifies the language elements on which the attribute can be placed.
AllowMultiple=allowmultiple, //If this is true, the attribute is multiuse
Inherited=inherited //If it is true, the attribute is inherited by derived classes
)]
Example:
[AttributeUsage(AttributeTargets.Class | //attribute can be applied on class
AttributeTargets.Constructor | //attribute can be applied on Constructor
AttributeTargets.Field | //attribute can be applied on Field
AttributeTargets.Method | //attribute can be applied on Method
AttributeTargets.Property, //attribute can be applied on Property
AllowMultiple = true)]
2). Conditional:
The Conditional Attribute allows you to create conditional methods. A conditional method is invoked only when a specific symbol has been defined via #define. Otherwise, the method is bypassed.
To use the Conditional attribute, you must include the System.Diagnostics namespace.
Conditional methods have a few restrictions.
Example:
#define TRIAL /*w w w .ja v a 2s. c o m*/
using System;
using System.Diagnostics;
class MainClass
{
[Conditional("TRIAL")]
void trial() {
Console.WriteLine("Trial version, not for distribution.");
}
[Conditional("RELEASE")]
void release()
{
Console.WriteLine("Final release version.");
}
public static void Main()
{
MainClass t = new MainClass();
t.trial(); // call only if TRIAL is defined
t.release(); // called only if RELEASE is defined
}
3). Obsolete:
Marks types and type members outdated
Example:
Obsolete attribute can be used with types or type members that are obsolete (Outdated). If a developer uses a type or a type member that is decorated with obsolete attribute, the compiler issues a warning or an error depending on how the attribute is configured.
using System;
using System.Collections.Generic;
public class MainClass
{
private static void Main()
{
Calculator.Add(10, 15);
}
}
public class Calculator
{
[Obsolete("Use Add(List Numbers) instead")]
public static int Add(int FirstNumber, int SecondNumber)
{
return FirstNumber + SecondNumber;
}
public static int Add(List Numbers)
{
int Sum = 0;
foreach (int Number in Numbers)
{
Sum = Sum + Number;
}
return Sum;
}
}
Note:
How can we create custom Attributes?
The “Obsolete” attribute which we discussed at the top is a readymade attribute To create a custom attributes you need to inherit from the attribute class. Below is a simple “HelpAttribute” which has a “HelpText” property.
Example:
class HelpAttribute : Attribute
{
public string HelpText { get; set; }
}
“HelpAttribute” is applied to the “Customer” as shown in the code below. Now developers who see this class , see the information right in the front of their eyes.
[Help(HelpText="This is a class")]
class Customer
{
private string _CustomerCode;
[Help(HelpText = "This is a property")]
public string CustomerCode
{
get { return _CustomerCode; }
set { _CustomerCode = value; }
}
[Help(HelpText = "This is a method")]
public void Add()
{
}
}
Is it possible to restrict a custom attribute to a method only?
By using the “AttributeUsage” and “AttributeTargets” you can restrict the attribute to a particular section like class , method , property etc. Below is a simple custom attribute is now confined only to methods.
[AttributeUsage(AttributeTargets.Method)]
class HelpAttribute : Attribute
{
public string HelpText { get; set; }
}
If you try to apply the above attribute over a class or property you would get a compile time error.
Custom attribute which describes length of characters for a property of a class.
[AttributeUsage(AttributeTargets.Property)]
class Check : Attribute
{
public int MaxLength { get; set; }
}
Below is a customer class over which that property is decorated providing information that the maximum length of “CustomerCode” property cannot exceed 10 character.
class Customer
{
private string _CustomerCode;
[Check(MaxLength = 10)]
public string CustomerCode
{
}
}
What if we want some attributes to be prevented from inheriting?
[AttributeUsage(AttributeTargets.Property,Inherited=false)]
class Check : Attribute
{
publicintMaxLength { get; set; }
}
If I want an attribute to be used only once in a program?
If you specify “AllowMultiple” as true it can be used multiple times in the same program.
[AttributeUsage(AttributeTargets.Property,AllowMultiple=false)]
class Check : Attribute
{
public int MaxLength { get; set; }
}
Attributes allow you to add declarative information to your programs. This information can then be queried at runtime using reflection. Atrributes can be applied on class, methods, assemblies etc. You You can add declarative information to a program by using an attribute. A declarative tag is depicted by square ([ ]) brackets placed above the element it is used for.
A few pre-defined attributes with in the .NET framework.
- Obsolete - Marks types and type members outdated
- WebMethod - To expose a method as an XML Web service method
- Serializable - Indicates that a class can be serialized
Predefined Types Of Atrributes
1). AttributeUsage
2). Conditional
3). Obsolete
1). AttributeUsage:
It specifies the types of items to which the attribute can be applied.
Syntax for specifying this attribute is as follows:
[AttributeUsage(
validon, //The parameter validon specifies the language elements on which the attribute can be placed.
AllowMultiple=allowmultiple, //If this is true, the attribute is multiuse
Inherited=inherited //If it is true, the attribute is inherited by derived classes
)]
Example:
[AttributeUsage(AttributeTargets.Class | //attribute can be applied on class
AttributeTargets.Constructor | //attribute can be applied on Constructor
AttributeTargets.Field | //attribute can be applied on Field
AttributeTargets.Method | //attribute can be applied on Method
AttributeTargets.Property, //attribute can be applied on Property
AllowMultiple = true)]
2). Conditional:
The Conditional Attribute allows you to create conditional methods. A conditional method is invoked only when a specific symbol has been defined via #define. Otherwise, the method is bypassed.
To use the Conditional attribute, you must include the System.Diagnostics namespace.
Conditional methods have a few restrictions.
- Conditional methods must return void.
- Conditional methods must be members of a class, not an interface.
- Conditional methods cannot be preceded with the override keyword.
Example:
#define TRIAL /*w w w .ja v a 2s. c o m*/
using System;
using System.Diagnostics;
class MainClass
{
[Conditional("TRIAL")]
void trial() {
Console.WriteLine("Trial version, not for distribution.");
}
[Conditional("RELEASE")]
void release()
{
Console.WriteLine("Final release version.");
}
public static void Main()
{
MainClass t = new MainClass();
t.trial(); // call only if TRIAL is defined
t.release(); // called only if RELEASE is defined
}
3). Obsolete:
Marks types and type members outdated
Example:
Obsolete attribute can be used with types or type members that are obsolete (Outdated). If a developer uses a type or a type member that is decorated with obsolete attribute, the compiler issues a warning or an error depending on how the attribute is configured.
using System;
using System.Collections.Generic;
public class MainClass
{
private static void Main()
{
Calculator.Add(10, 15);
}
}
public class Calculator
{
[Obsolete("Use Add(List
public static int Add(int FirstNumber, int SecondNumber)
{
return FirstNumber + SecondNumber;
}
public static int Add(List
{
int Sum = 0;
foreach (int Number in Numbers)
{
Sum = Sum + Number;
}
return Sum;
}
}
Note:
How can we create custom Attributes?
The “Obsolete” attribute which we discussed at the top is a readymade attribute To create a custom attributes you need to inherit from the attribute class. Below is a simple “HelpAttribute” which has a “HelpText” property.
Example:
class HelpAttribute : Attribute
{
public string HelpText { get; set; }
}
“HelpAttribute” is applied to the “Customer” as shown in the code below. Now developers who see this class , see the information right in the front of their eyes.
[Help(HelpText="This is a class")]
class Customer
{
private string _CustomerCode;
[Help(HelpText = "This is a property")]
public string CustomerCode
{
get { return _CustomerCode; }
set { _CustomerCode = value; }
}
[Help(HelpText = "This is a method")]
public void Add()
{
}
}
Is it possible to restrict a custom attribute to a method only?
By using the “AttributeUsage” and “AttributeTargets” you can restrict the attribute to a particular section like class , method , property etc. Below is a simple custom attribute is now confined only to methods.
[AttributeUsage(AttributeTargets.Method)]
class HelpAttribute : Attribute
{
public string HelpText { get; set; }
}
If you try to apply the above attribute over a class or property you would get a compile time error.
Custom attribute which describes length of characters for a property of a class.
[AttributeUsage(AttributeTargets.Property)]
class Check : Attribute
{
public int MaxLength { get; set; }
}
Below is a customer class over which that property is decorated providing information that the maximum length of “CustomerCode” property cannot exceed 10 character.
class Customer
{
private string _CustomerCode;
[Check(MaxLength = 10)]
public string CustomerCode
{
}
}
What if we want some attributes to be prevented from inheriting?
[AttributeUsage(AttributeTargets.Property,Inherited=false)]
class Check : Attribute
{
publicintMaxLength { get; set; }
}
If I want an attribute to be used only once in a program?
If you specify “AllowMultiple” as true it can be used multiple times in the same program.
[AttributeUsage(AttributeTargets.Property,AllowMultiple=false)]
class Check : Attribute
{
public int MaxLength { get; set; }
}
Comments
Post a Comment