FAQ: How do I retrieve assembly attributes at runtime?
-
Tuesday, April 14, 2009 11:58 AM
How do I retrieve assembly attributes at runtime?
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
All Replies
-
Tuesday, April 14, 2009 12:00 PM
Most of the assembly-wide attributes (which are usually specified in AssemblyInfo.cs) are available using Assembly.GetCustomAttributes. For instance, to get the assembly title, you could use code such as:
Assembly asm = this.GetType().Assembly; object[] attrs = asm.GetCustomAttributes(typeof(AssemblyTitleAttribute), false)); if (attrs.Length == 1) { Console.WriteLine(((AssemblyTitleAttribute)attrs[0]).Title); }Note, the AssemblyVersionAttribute ends up as just part of the assembly name, which can be retrieved with the Assembly.GetName method. The version can be retrieved from the name with the Version property. For example:
Assembly asm = this.GetType().Assembly; Console.WriteLine(asm.GetName().Version);
Related threads:
Reflection and Custom Attributes
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/6d8dc53d-9238-4387-997f-46013e5fa99b/
What method can be used to get the list of all the attributes present in a class in C#
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/16583130-d700-4eb9-9979-d5c955efd899/
For more FAQ about Base Class Library, please see Base Class Library FAQ
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked As Answer by Xiaoyun Li – MSFT Tuesday, April 14, 2009 12:00 PM

