Geeks With Blogs

AzamSharp Some day I will know everything. I hope that day never comes.

Okay I was kinda getting bored so I thought of writing a simple Custom Attribute. This custom attribute simply prints out the attribute information that the user defines on the method. The information contains the method author, description and the date created.

Here is the Custom Attribute Class:

using System;
using System.Collections.Generic;
using System.Text;

namespace TestingConsoleApplication
class CodeHistory : System.Attribute 
private string _author;
private string _description;
private DateTime _dateCreated;

public CodeHistory(string author, 
string description, string dateCreated)
            _author = author;
            _description = description;
            _dateCreated = DateTime.Parse(dateCreated); 

public string Author
get return _author; }
set { _author = value; } 

public string Description
get return _description; }
set { _description = value; } 

public DateTime DateCreated
get return _dateCreated; }
set { _dateCreated = value; } 

And here are the methods that uses the CodeHistory attributes:

[CodeHistory("Mohammad Azam","Returns the Customer Name","02/23/2006")]
public string GetCustomerName()
return "JohnDoe"; 

        [CodeHistory("Peter Griffen"
        ,"This method loads something","02/24/2006")]
public void LoadDetails()
// Does someting

And finally here is the method that prints out the information attribute information and also the name of the method on which the attribute is applied:

 public static void LoadAssemblyAndPrintDetails(string assemblyName)
     Assembly assembly = Assembly.Load(assemblyName);

            Type[] types = assembly.GetTypes();

foreach (Type type in types)
                MethodInfo[] methodInfo = type.GetMethods();

foreach (MethodInfo method in methodInfo)
object[] methodAtt =

foreach (Attribute att in methodAtt)
                        CodeHistory codeHis = (CodeHistory)att;

     Console.WriteLine("Method Name: {0}", method.Name); 
     Console.WriteLine("Author: {0}",codeHis.Author);
     Console.WriteLine("Description: {0}", codeHis.Description);
     Console.WriteLine("Date Created: {0}", codeHis.DateCreated);





powered by IMHO 1.3

Posted on Thursday, February 23, 2006 9:36 PM | Back to top

Comments on this post: Custom Attributes

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Mohammad Azam | Powered by: