Introduction
Usinng an attribute is a way to add special meaning to our method and cause it to act in a certain way. Before this was available, developers didn't have a way to define their own attributes. DotNet paved the way for developers and opened new horizons to conquer. Attributes are like adding behaviours to methods, classes, properties structures, events, modules, and so forth. It means we can enforce certain constraints on those methods, classes, properties and vice-versa to behave in the way specify to them.
(continued)
Let us start to learn to use attributes in C# code:
Case 1
Using System;
Namespace MyExample
{
Class MyAttribute
{
[Obsolete()]
public static void SaySomething(string str)
{
Console.WriteLine(str);
}
public static void Talk(string str)
{
Console.WriteLine(str);
}
static void Main()
{
SaySomething("Hello to Sufyan");
Console.ReadLine();
}
}
}
[Obsolete("SaySomething() method is now Obsolete.
Please use Talk()")]
By using this approach, we can add additional meaning to our attribute.
Note: We can have multiple attribute statements before a method or class. In such cases, if one of the statements is true, it will allow access to that particular method.
There are many attributes that come with DotNet.
Conditional attribute
Case 2
[Conditional(.DEBUG.)]
public static void Help(String str)
{
Console.WriteLine(str);
}
DLL Import
Case 3
using System;
System.Runtime.InteropServices;
class Beeper
{
[DllImport(.kernel32.dll.)]
Public static extern bool Beep(int frequency,int duration);
static void Main()
{
Beep(1000,111);
}
}
Steps in Creating a Custom Attribute
- Define the attribute's usage.
- Extend our class with AttributeClass.
- Define the behaviours to the class.
Attribute behaviours can be of these types:
- All—Any application element
- Assembly—Attribute can be applied to an assembly
- Class—Attribute can be applied to a class
- Constructor—Attribute can be applied to a constructor
- Delegate—Attribute can be applied to a delegate
- Enum—Attribute can be applied to an enumeration
- Event—Attribute can be applied to an event
- Field—Attribute can be applied to a field
- Interface—Attribute can be applied to an interface
- Method—Attribute can be applied to a method
- Module—Attribute can be applied to a module
- Parameter—Attribute can be applied to a parameter
- Property—Attribute can be applied to a property
- ReturnValue—Attribute can be applied to a return value
- Struct—Attribute can be applied to a structure
...
public class MySpecialAttribute:Attribute
{...
using System;
namespace RegKeyAttributeTestor
{
[AttributeUsage(AttributeTargets.Method|AttributeTargets.Struct,
AllowMultiple=false,Inherited=false]
public class MyAttribute:Attribute
{
private string regKey="a12nf";
public MyAttribute(string regKey)
{
if(this.regKey==regKey)
{
Console.WriteLine("Permitted to use this App");
}
else
{
Console.WriteLine("Not registered to use this App");
}
}
} //End Attribute class code
class useAttrib
{
[MyAttribute("hello")]
public static string SayHello(string str)
{
return str;
}
static void Main()
{
Console.WriteLine(SayHello("Hello to Sufyan"));
Console.ReadLine();
}
}
}
AttributeUsage(AttributeTargets.Method|AttributeTargets.Struct,
AllowMultiple=false, Inherited=false)]
Reading Metadata from Assemblies
We first write our custom attribute:
using System;
namespace Sufyan
{
[AttributeUsage(AttributeTargets.Method,AllowMultiple=false,
Inherited=false)]
public class RegKeyAttribute : Attribute
{
private string regKey;
public RegKeyAttribute(string VRegKey)
{
this.regKey = VRegKey;
if (this.regKey==.hello.)
{
Console.WriteLine("Aha! You're Registered");
}
else
{
Console.WriteLine("Oho! You're not Registered");
}
}
}
}
Now, we shall write our code to reference it through Assembly;.
Steps
using System;
using System.Reflection;
using Sufyan;
namespace AdvancedDotNet1
{
class Classic
{
static void Main(string[] args)
{
Assembly suf = Assembly.Load("RegKey");
Type KIA=suf.GetType();
[]KO=Attribute.GetCustomAttributes(KIA);
Object Regist =KO[0];
Console.WriteLine("Registeration Code is :"+
Regist.ToString() );
Console.ReadLine();
}
}
}
System.Reflection is used to retrieve info from the assembly metadata.
class useAttrib
{
[RegKeyAttribute ("hello")]
public static string SayHello(string str)
{
return str;
}
static void Main()
{
Console.WriteLine(SayHello("Hello to Sufyan"));
Console.ReadLine();
}
}
浙公网安备 33010602011771号