Click here to Skip to main content
Licence 
First Posted 8 Feb 2004
Views 57,781
Downloads 963
Bookmarked 23 times

Assembly Attributes

By Ahmed Alhosaini | 8 Feb 2004
Programmatically inspecting assembly attributes.
5 votes, 41.7%
1
2 votes, 16.7%
2
1 vote, 8.3%
3
1 vote, 8.3%
4
3 votes, 25.0%
5
2.94/5 - 12 votes
μ 2.54, σa 3.03 [?]

Sample Image - Attributes.jpg

Introduction

Reflection allows you to programmatically inspect an assembly, and get information about the assembly, including all object types contained within. This information includes the attributes you have added to those types. The reflection objects reside within the System.Reflection namespace.

This demo program will inspect an assembly and display a list of all attributes defined on the assembly. The entire source code is downloadable and I will explain how it works.

Source Code Explanation

The code first checks the parameters passed to the command-line - if none are supplied, or if the user types FindAttributes /? then the Usage() method will be called, which will display a simple command usage summary:

if (args.Length == 0)
      Usage();
else if((args.Length == 1)&&(args[0] == "/?"))
      Usage();

Next, we reconstitute the command-line arguments into a single string. The reason for this is that it's common to have spaces in directory names, such as "Program Files", and this would be considered as two arguments by virtue of there being a space. So, we iterate through all the arguments stitching them back into a single string, and use this as the name of the assembly to load:

foreach(string arg in args)
      {
        if(assemblyName == null)
          assemblyName = arg;
        else
          assemblyName = string.Format("{0} {1}", assemblyName, arg);
      }

We then attempt to load the assembly, and retrieve all custom attributes defined on that assembly with the GetCustomAttributes() method:

Assembly a = Assembly.LoadFrom(assemblyName);

// Now find the attributes on the assembly
object[] attributes = a.GetCustomAttributes(true) ;

// If there were any attributes defined...
if(attributes.Length > 0)
   {
      Console.WriteLine("Assembly attributes for '{0}'...", assemblyName);
      // Dump them out...
      foreach(object o in attributes)
           Console.WriteLine("  {0}", o.ToString());
   }

Any attributes that are found are output to the console.

You can build the executable of the source code by using the command-line compiler like this:

>csc FindAttributes.cs

Running the Program

When you test the program against the FindAttributes.exe file, an attribute called DebuggableAttribute will be displayed, as shown in the screen shot. Although this attribute was not specified, it has been added by the C# compiler, and you will find that most of your executables have this attribute.

Additional Notes

You can alter the code as appropriate to add on as many assembly attributes as you wish. As an example, try modifying the source code by un-commenting the line after the using directives at the beginning of the listing.

  //[assembly: AssemblyTitle("Title")]

When you recompile and run the code, you will see an additional attribute has been added to the list, the System.Reflection.AssemblyTitleAttribute.

Please check Karli Watson's Beginning Visual C# [ISBN 1-86100-758-2] by Wrox Press for more information.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Ahmed Alhosaini

Architect

United States United States

Member
Ahmed had his M.S. Degree in Electrical and Computer Engineering in USA and the B.Sc. Degree in Automatic Control and Computer Systems Engineering in Egypt. He programmed with Assembly, Fortran77, Prolog, C/C++, Microsoft Visual Basic, Microsoft Visual C++, ATL/COM, Microsoft Visual C#, VB.NET, ASP.NET, AJAX, SharePoint 2007/2010, Microsoft Commerce Server, and MATLAB and Maple for technical computing. His programming experience is about 15+ years and he is a Microsoft Certified Professional. He also has a working experience in Database technologies and administration of SQL Server databases on Windows NT/2000/2003/2008 Windows Server platforms. Ahmed is interested in developing enterprise business solutions and has participated in many business, services and consultancy projects.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
NewsHi Ahmad PinmemberCandseeme8:45 25 Feb '06  
GeneralGood job PinmemberAhmed Yassin9:52 26 Feb '06  
GeneralCustom Assembly Attributes Pinmemberdnh11:23 21 Apr '04  
GeneralRe: Custom Assembly Attributes PinmemberAhmad Yassin12:27 21 Apr '04  
GeneralRe: Custom Assembly Attributes Pinmemberdnh9:47 22 Apr '04  
GeneralSome minor issues PinsupporterVictor Boctor13:23 9 Feb '04  
Command line:
Should paths that include spaces be enclosed in quotation marks? The current technique used does not allow for adding further command line options in the future.
 
Typos:
modefying -> modifying
BSc. -> B.Sc.
He Also -> He also
governamental -> governamental
consultency -> consultancy
 
phpWebNotes is a page annotation system modelled after php.net.
http://webnotes.sourceforge.net/demo.php[^]

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 9 Feb 2004
Article Copyright 2004 by Ahmed Alhosaini
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid