Attribute Class

Language: C#

Code:

using System;

[AttributeUsage(AttributeTargets.All)]

public class <AttributeName> : Attribute

{

public <AttributeName>(String Name) //if no named parameter is there no need of this constructor

{

this.Name=Name;

}

public string Name { get; set; } //Named Parameter i.e it is compulsory

public string[] stringarray{ get; set; } // Optional Parameter

}

[<Attributename>(“xyz”)]

public class <class name>

{

//Class Definition goes here

}

[<Attributename>(“xyz”,stringarray = new string[] {“Hi” , “Hello” })]

public class <class name1>

{

//Class Definition goes here

}

public class <class name2>

{

System.Attribute[] attrs =   System.Attribute.GetCustomAttributes(typeof(<classname1>));

foreach (System.Attribute attribute in attrs)

{

if (attribute is <AttributeName>)

{

<AttributeName> e = (<AttributeName>)attribute;

Console.WriteLine(e.Name);

Console.WriteLine(e.stringarray.Count());

}

}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s