This blog is about the dotnet.all types of codes,news about dotnet including asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.ncluding asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.asp.net programming,dot net programming,dotnet programs,dotnet source code,source code.

Free Hosting

Free Hosting

Tuesday, November 11, 2008

how to create custom attribute classes, use them in code, and query them.

This article shows how to create custom attribute classes, use them in code, and query them.

Attributes provide a powerful method of associating declarative information with C# code (types, methods, properties, and so forth). Once associated with a program entity, the attribute can be queried at run time and used in any number of ways.

We can create a custom attribute by defining an attribute class, a class that derives directly or indirectly from System.Attribute.

The below given code shows how to declare and use an attribute. This code returns the name of the author and the version of the type to which the attribute is assigned.

Note:
AttributeUsage? specifies the language elements to which the attribute can be applied GetCustomAttributes? returns an array of objects that are the run-time equivalents of the source code attributes.

(Copy the code in a text editor save it as a .cs file and compile using "csc filename.cs")

Code:


using System;

namespace Attributes
{

class Class1
{

AttributeUsage(AttributeTargets.All)
public class Details : Attribute
{
public readonly string MemberName?;
public double Version = 1.0;
public Details(string name)
{
this.MemberName = name;
}
public double version
{
get
{
return Version;
}
set
{
Version = value;
}
}
}
Details("Rajadurai")
public interface Iattribute
{
void display();
}
Details("Sankar")
public class classAttribute1 : Iattribute
{
public void display()
{
Console.WriteLine("My Author is Sankar");
}
}

static void Main(string[] args)
{
processAttributes p = new processAttributes(typeof(classAttribute1));
processAttributes q = new processAttributes(typeof(Iattribute));
//processAttributes r = new processAttributes(typeof(methodAttribute1));
int i = Console.Read();
}

public class processAttributes
{
public processAttributes(Type t)
{
Attribute[] attrs = Attribute.GetCustomAttributes(t);
foreach(Attribute attr in attrs)
{
if (attr is Details)
{
Console.WriteLine("Type Name :{0}",t);
Details d = (Details)attr;
Console.WriteLine("Author Name :{0}",d.MemberName);
Console.WriteLine("Version :{0}",d.version);
Console.WriteLine("");
}
}
}
}
}
}

0 comments:

dotnet(.Net) Project Source code Downloads and Tutorials

Email Subscrption



Enter your email address:

Delivered by FeedBurner

Feedburner Count

Blog Archive

Unique Visitor

Design by araba-cı | MoneyGenerator Blogger Template by GosuBlogger