Search code examples
c#assemblyinfo

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


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.


Solution

  • 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!