tags:

views:

44

answers:

3

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!

A: 

Get Type instance of any type from this library. then from this Type get Assembly Instance. Then get Version.

Trickster
I'm new to Reflection. Can you spell that out to me in code? I've been trying to trace that down but just haven't been able to figure it out. Thanks!
Blake Blackwell
typeof(LClass).Assembly.GetName().Version
Trickster
Version is a different attribute than Company
Rex M
+ 1 Yes you right i missed this. anyway he need to get assembly first =)
Trickster
+4  A: 

To get the assembly in which your current code (the class library code) actually resides, and read its company attribute:

Assembly currentAssem = typeof(CurrentClass).Assembly;
object[] attribs = currentAssem.GetCustomAttributes(typeof(AssemblyCompanyAttribute), true);
if(attribs.Length > 0)
{
    string company = ((AssemblyCompanyAttribute)attribs[0]).Company
}
Rex M
according to the prophecy...
Chris Ballance
I don't know if this is because I'm in .NET 2.0, but I had to add "true" to the end of call for GetCustomAttributes. Other than that, worked like a champ! Thanks for your help!
Blake Blackwell
+1  A: 
    Assembly assembly = typeof(CurrentClass).GetAssembly();
    AssemblyCompanyAttribute companyAttribute = AssemblyCompanyAttribute.GetCustomAttribute(assembly, typeof(AssemblyCompanyAttribute)) as AssemblyCompanyAttribute;
    if (companyAttribute != null)
    {
        string companyName = companyAttribute.Company;
        // Do something
    }
Philip Wallace
If you're going to downvote - tell me why!
Philip Wallace
you forgot to check companyAttribute == null
Trickster
And thats was not my -1. =)
Trickster
Fixed. The code I took this from doesn't check for null because it is in our logging component (where the company name attribute IS set). The company name is required, so if it is not there an exception will be thrown the first time the offending developer runs the code!
Philip Wallace