Retrieve AssemblyCompanyName from Class Library

Viewed 9129

I would like to get the AssemblyCompany attribute from a WinForm project inside of my C# class library. In WinForms, I can get to this information by using:

Application.CompanyName;

However, I can't seem to find a way to get at that same information using a class library. Any help you could provide would be great!

6 Answers

The dotnet 5 version:

typeof(CurrentClass).GetCustomAttribute<AssemblyCompanyAttribute>().Company

You can also get it using LINQ :

public static string GetAssemblyCompany<T>(){
    string company = (from Attribute a in (typeof(T).Assembly.GetCustomAttributes())
        where a is AssemblyCompanyAttribute
        select ((AssemblyCompanyAttribute)a).Company).FirstOrDefault();

    return company;
}

This is a great way to get what you are looking for in a single line:

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