Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read assembly attributes

In my program, how can I read the properties set in AssemblyInfo.cs:

[assembly: AssemblyTitle("My Product")] [assembly: AssemblyDescription("...")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Radeldudel inc.")] [assembly: AssemblyProduct("My Product")] [assembly: AssemblyCopyright("Copyright @ me 2008")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] 

I'd like to display some of these values to the user of my program, so I'd like to know how to load them from the main program and from komponent assemblies I'm using.

like image 573
Sam Avatar asked Oct 09 '08 14:10

Sam


People also ask

What are assembly attributes?

Assembly attributes are values that provide information about an assembly. The attributes are divided into the following sets of information: Assembly identity attributes. Informational attributes.

Where do I put assembly attributes?

Assembly attributes like that must be before any namespace or class (or other type) declarations in the code file in question. Show activity on this post. I'd usually put it outside of any class declarations, and indeed namespace declarations. It could go anywhere (except within a class), and AssemblyInfo.

What are the attributes of AssemblyInfo CS file?

Assembly identity attributes Three attributes (with a strong name, if applicable) determine the identity of an assembly: name, version, and culture. These attributes form the full name of the assembly and are required when you reference it in code. You can set an assembly's version and culture using attributes.

Which file contains information about the attribute information of an assembly?

An assembly manifest contains all the metadata needed to specify the assembly's version requirements and security identity, and all metadata needed to define the scope of the assembly and resolve references to resources and classes.


2 Answers

This is reasonably easy. You have to use reflection. You need an instance of Assembly that represents the assembly with the attributes you want to read. An easy way of getting this is to do:

typeof(MyTypeInAssembly).Assembly 

Then you can do this, for example:

object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false);  AssemblyProductAttribute attribute = null; if (attributes.Length > 0) {    attribute = attributes[0] as AssemblyProductAttribute; } 

Referencing attribute.Product will now give you the value you passed to the attribute in your AssemblyInfo.cs. Of course, if the attribute you look for can occur more than once, you may get multiple instances in the array returned by GetCustomAttributes, but this is not usually an issue for assembly level attributes like the ones you hope to retrieve.

like image 52
Jeff Yates Avatar answered Sep 20 '22 10:09

Jeff Yates


I've created this extension method that uses Linq:

public static T GetAssemblyAttribute<T>(this System.Reflection.Assembly ass) where T :  Attribute {     object[] attributes = ass.GetCustomAttributes(typeof(T), false);     if (attributes == null || attributes.Length == 0)         return null;     return attributes.OfType<T>().SingleOrDefault(); } 

and then you can conveniently use it like that:

var attr = targetAssembly.GetAssemblyAttribute<AssemblyDescriptionAttribute>(); if(attr != null)      Console.WriteLine("{0} Assembly Description:{1}", Environment.NewLine, attr.Description); 
like image 20
dmihailescu Avatar answered Sep 23 '22 10:09

dmihailescu