获取程序集信息

/// <summary>
/// 返回程序集的Title
/// </summary>
/// <param name="assembly">程序集</param>
/// <returns>Title</returns>
public static  string GetAssemblyTitle(Assembly assembly)
{
    string title = GetAssemblyInfo<AssemblyTitleAttribute>(assembly );
    if(string.IsNullOrEmpty(title))
    {
        return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
    }
    return title;
}

/// <summary>
/// 返回程序集的版本信息
/// </summary>
/// <param name="assembly">程序集</param>
/// <returns>版本信息</returns>
public static string GetAssemblyVersion(Assembly assembly)
{
    return assembly.GetName().Version.ToString();
}
/// <summary>
/// 返回程序集的文本说明
/// </summary>
/// <param name="assembly">程序集</param>
/// <returns>文本说明</returns>
public static string GetAssemblyDescription(Assembly assembly)
{
    return GetAssemblyInfo<AssemblyDescriptionAttribute>(assembly);
}

/// <summary>
/// 返回程序集的产品名
/// </summary>
/// <param name="assembly">程序集</param>
/// <returns>产品名</returns>
public static string GetAssemblyProduct(Assembly assembly)
{
    return GetAssemblyInfo<AssemblyProductAttribute>(assembly);
}

/// <summary>
/// 返回程序集的版权
/// </summary>
/// <param name="assembly">程序集</param>
/// <returns>版权</returns>
public static string GetAssemblyCopyright(Assembly assembly)
{
    return GetAssemblyInfo<AssemblyCopyrightAttribute>(assembly);
}

/// <summary>
/// 返回程序集的公司名称
/// </summary>
/// <param name="assembly">程序集</param>
/// <returns>公司名称</returns>
public static string GetAssemblyCompany(Assembly assembly)
{
    return GetAssemblyInfo<AssemblyCompanyAttribute>(assembly);
}

/// <summary>
/// 获取程序集的相关信息
/// </summary>
/// <typeparam name="TAttribute">特性类型</typeparam>
/// <param name="assembly">程序集</param>
/// <returns>返回的信息。如果没有找到,则返回特性类型名称;如果不具有相关特性,则返回空白字符串</returns>
public static string GetAssemblyInfo<TAttribute>(Assembly assembly)where TAttribute : Attribute
{
    Type type =typeof(TAttribute);
    object[] attributes = assembly.GetCustomAttributes(type, false);
    if(attributes!=null&& attributes.Length>0)
    {
        PropertyInfo[]  propertyInfos = type.GetProperties();
        foreach (PropertyInfo property in propertyInfos)
        {
            if (typeof(string).Equals(property.PropertyType))
            {
                return property.GetValue(attributes[0]).ToString();
            }
        }
    }
    return string.Empty;
}

 

posted @ 2020-04-17 12:58  一只独行的猿  阅读(301)  评论(0编辑  收藏  举报