Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve the 'AssemblyCompany' setting (in AssemblyInfo.cs)? [duplicate]

Is it possible to retrieve this value at runtime?

I'd like to keep the value in one place, and retrieve it whenever my application needs to output the name of my company.

like image 749
Jonathan Avatar asked Jun 27 '10 12:06

Jonathan


People also ask

What are the contents of AssemblyInfo CS file?

AssemblyInfo. cs contains information about your assembly, like name, description, version, etc. You can find more details about its content reading the comments that are included in it.

Where can I find AssemblyInfo CS?

It is located under the folder "Properties". AssemblyInfo. cs file also get created, when you create any Web Application project.

How do I add AssemblyInfo to my project?

You can generate an assemblyInfo. cs by right clicking the project and chosing properties. In the application tab fill in the details and press save, this will generate the assemblyInfo.


2 Answers

This should do it:

using System.Reflection;  string company = ((AssemblyCompanyAttribute)Attribute.GetCustomAttribute(     Assembly.GetExecutingAssembly(), typeof(AssemblyCompanyAttribute), false))    .Company; 

If you really want to keep the value in one place and you have multiple assemblies in your solution, you could either:

  • Use GetEntryAssembly instead of GetExecutingAssembly and set the company attribute only on your entry assembly, or better:
  • Use a central assembly info file, see this answer

UPDATE Improved the code by suggestion of @hmemcpy so it doesn't need [0] anymore. Thanks!

like image 54
Sandor Drieënhuizen Avatar answered Oct 07 '22 16:10

Sandor Drieënhuizen


If you want a nice generic method for fetching the assembly Info data try the following:

public string GetAssemblyAttribute<T>(Func<T, string> value)     where T : Attribute {     T attribute = (T)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof (T));     return value.Invoke(attribute); } 

So you can then call

string title = GetAssemblyAttribute<AssemblyTitleAttribute>(a => a.Title); string copyright = GetAssemblyAttribute<AssemblyCopyrightAttribute>(a => a.Copyright); string version = GetAssemblyAttribute<AssemblyVersionAttribute>(a => a.Version); string description = GetAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description); 
like image 30
Adam S-Price Avatar answered Oct 07 '22 17:10

Adam S-Price