Click here to Skip to main content
15,898,035 members
Articles / Programming Languages / C#

Creating and Using Attributes in your .NET application

Rate me:
Please Sign up or sign in to vote.
4.75/5 (51 votes)
9 Feb 2002BSD9 min read 570K   4.8K   187  
Shows how to use existing attributes and how to create and use your own attributes
using System;

namespace Test
{
	public class Driver
	{
		// Entry point of the program
		public static void Main(string [] Args)
		{
			// We really don't have to create an instance of TestClass*, remember
			// attributes are properties applied to Type's and the parts that make 
			// up types (fields, properties, methods, return values, parameters).
			TestClassA a = new TestClassA();
			TestClassB b = new TestClassB();
			string c = "";

			PrintTestAttributes(a);		// Should print out the number 3 and an empty string, since
										// none was specified on the class
			PrintTestAttributes(b);		// Should print out the number 4 and the name of the class
			PrintTestAttributes(c);		// Shouldn't print out that TestAttribute exists
		}
		
		// Prints out the values for the TestAttribute on the object
		// if the TestAttribute isn't applied it prints out a message
		// indicating such
		public static void PrintTestAttributes(object obj)
		{
			Type type = obj.GetType();	// Get the underlying type from the object passed in
										// attributes are decorations on Type's so we need
										// a Type object to operate on.

			// Get the attributes applied to this object of type
			// TestAttribute, this returns an array of TestAttributes, one for 
			// each TestAttribute applied to the class.
			// The code will only expect 0 or 1 TestAttributes to be applied
			// The false parameter tells the runtime to only search this type, none of the base types for the attribute
			TestAttribute [] AttributeArray = (TestAttribute []) type.GetCustomAttributes(typeof(TestAttribute), false);
			
			// Class name
			Console.WriteLine("Class:\t{0}", type.Name);

			// If no attributes were returned, print out the message
			if( AttributeArray.Length == 0 )
			{
				Console.WriteLine("There are no TestAttributes applied to this class");
				return ;
			}

			// Retrieve the one and only instance of TestAttribute from class
			TestAttribute ta = AttributeArray[0];
			
			// Called the TestAttribute's PrintOut method
			ta.PrintOut();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Software Developer (Senior) InfoPlanIT, LLC
United States United States
James has been programming in C/C++ since 1998, and grew fond of databases in 1999. His latest interest has been in C# and .NET where he has been having fun writing code starting when .NET v1.0 was in its first beta.

He is currently a senior developer and consultant for InfoPlanIT, a small international consulting company that focuses on custom solutions and business intelligence applications.

He was previously employed by ComponentOne where he was a Product Manager for the ActiveReports, Data Dynamics Reports, and ActiveAnalysis products.

Code contained in articles where he is the sole author is licensed via the new BSD license.

Comments and Discussions