views:

50

answers:

1

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.

+4  A: 

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!

Sandor Drieënhuizen
This could be a little nicer using `Attribute.GetCustomAttribute()` method. It hides the ugly `[0]` indexer.
hmemcpy
@hmemcpy: Great!
abatishchev
@hmemcpy: thanks, great suggestion. I've updated my answer accordingly.
Sandor Drieënhuizen