从类库中检索AssemblyCompanyName

时间:2009-10-26 19:20:40

标签: c# .net

我想从我的C#类库中的WinForm项目中获取AssemblyCompany属性。在WinForms中,我可以使用以下命令获取此信息:

Application.CompanyName;

但是,我似乎找不到使用类库获取相同信息的方法。你能提供的任何帮助都会很棒!

6 个答案:

答案 0 :(得分:28)

获取当前代码(类库代码)实际所在的程序集,并读取其公司属性:

Assembly currentAssem = typeof(CurrentClass).Assembly;
object[] attribs = currentAssem.GetCustomAttributes(typeof(AssemblyCompanyAttribute), true);
if(attribs.Length > 0)
{
    string company = ((AssemblyCompanyAttribute)attribs[0]).Company
}

答案 1 :(得分:5)

您可以使用FileVersionInfo类来获取更多公司名称。

    Dim info = FileVersionInfo.GetVersionInfo(GetType(AboutPage).Assembly.Location)
    Dim companyName = info.CompanyName
    Dim copyright = info.LegalCopyright
    Dim fileVersion = info.FileVersion

答案 2 :(得分:4)

    Assembly assembly = typeof(CurrentClass).GetAssembly();
    AssemblyCompanyAttribute companyAttribute = AssemblyCompanyAttribute.GetCustomAttribute(assembly, typeof(AssemblyCompanyAttribute)) as AssemblyCompanyAttribute;
    if (companyAttribute != null)
    {
        string companyName = companyAttribute.Company;
        // Do something
    }

答案 3 :(得分:0)

您也可以使用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;
}

答案 4 :(得分:0)

这是单行获取所需内容的好方法:

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

答案 5 :(得分:0)

dotnet 5 版本:

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