获取程序集信息(Assembly)

作者:追风剑情 发布于:2019-7-1 10:31 分类:C#

右键->属性->应用程序->程序集信息

1111.png

2222.png

示例


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace AssemblyInfo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(GetAssemblyInfo());

            Console.ReadKey();
        }

        public static String GetAssemblyInfo()
        {
            Type[] types = new Type[]
            {
                typeof(AssemblyTitleAttribute),//名称
                typeof(AssemblyDescriptionAttribute),//描述
                typeof(AssemblyCompanyAttribute),//公司
                typeof(AssemblyProductAttribute),//产品
                typeof(AssemblyCopyrightAttribute),//版权
                typeof(AssemblyTrademarkAttribute),//商标
                typeof(AssemblyVersionAttribute),//程序集版本
                typeof(AssemblyFileVersionAttribute),//文件版本
                typeof(AssemblyCultureAttribute)//区域性
            };

            StringBuilder sb = new StringBuilder();
            Type t = typeof(Program);
            for (int i=0; i<types.Length; i++)
            {
                Type type = types[i];
                object[] objs = type.Assembly.GetCustomAttributes(type, true);
                if (objs == null || objs.Length <= 0)
                    continue;
                object o = objs[0];

                AssemblyTitleAttribute title = o as AssemblyTitleAttribute;
                AssemblyDescriptionAttribute desc = o as AssemblyDescriptionAttribute;
                AssemblyCompanyAttribute company = o as AssemblyCompanyAttribute;
                AssemblyProductAttribute product = o as AssemblyProductAttribute;
                AssemblyCopyrightAttribute copyright = o as AssemblyCopyrightAttribute;
                AssemblyTrademarkAttribute trademark = o as AssemblyTrademarkAttribute;
                AssemblyVersionAttribute version = o as AssemblyVersionAttribute;
                AssemblyFileVersionAttribute fileversion = o as AssemblyFileVersionAttribute;
                AssemblyCultureAttribute culture = o as AssemblyCultureAttribute;

                if (title != null)
                    sb.AppendFormat("名称: {0}\n", title.Title);
                if (desc != null)
                    sb.AppendFormat("描述: {0}\n", desc.Description);
                if (company != null)
                    sb.AppendFormat("公司: {0}\n", company.Company);
                if (product != null)
                    sb.AppendFormat("产品: {0}\n", product.Product);
                if (copyright != null)
                    sb.AppendFormat("版权: {0}\n", copyright.Copyright);
                if (trademark != null)
                    sb.AppendFormat("商标: {0}\n", trademark.Trademark);
                if (version != null)
                    sb.AppendFormat("程序集版本: {0}\n", version.Version);
                if (fileversion != null)
                    sb.AppendFormat("文件版本: {0}\n", fileversion.Version);
                if (culture != null)
                    sb.AppendFormat("区域性: {0}\n", culture.Culture);
            }

            string GUID = t.GUID.ToString();
            bool IsCOMObject = t.IsCOMObject;

            sb.AppendFormat("GUID: {0}\n", GUID);
            sb.AppendFormat("ComVisible: {0}\n", IsCOMObject);

            return sb.ToString();
        }
    }
}


运行测试

1111.png

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号