- Predefined
- Customs
. Net Framework have Predefined attributes
- AttributeUsage
- Conditional
- 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();
}
}
'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
{
}
GetCustom attribute
Obsolete attribute
/*example pending**************/
No comments:
Post a Comment