获取程序集信息(Assembly)

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

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

1111.png

2222.png

示例


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;
  6.  
  7. namespace AssemblyInfo
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Console.WriteLine(GetAssemblyInfo());
  14.  
  15. Console.ReadKey();
  16. }
  17.  
  18. public static String GetAssemblyInfo()
  19. {
  20. Type[] types = new Type[]
  21. {
  22. typeof(AssemblyTitleAttribute),//名称
  23. typeof(AssemblyDescriptionAttribute),//描述
  24. typeof(AssemblyCompanyAttribute),//公司
  25. typeof(AssemblyProductAttribute),//产品
  26. typeof(AssemblyCopyrightAttribute),//版权
  27. typeof(AssemblyTrademarkAttribute),//商标
  28. typeof(AssemblyVersionAttribute),//程序集版本
  29. typeof(AssemblyFileVersionAttribute),//文件版本
  30. typeof(AssemblyCultureAttribute)//区域性
  31. };
  32.  
  33. StringBuilder sb = new StringBuilder();
  34. Type t = typeof(Program);
  35. for (int i=0; i<types.Length; i++)
  36. {
  37. Type type = types[i];
  38. object[] objs = type.Assembly.GetCustomAttributes(type, true);
  39. if (objs == null || objs.Length <= 0)
  40. continue;
  41. object o = objs[0];
  42.  
  43. AssemblyTitleAttribute title = o as AssemblyTitleAttribute;
  44. AssemblyDescriptionAttribute desc = o as AssemblyDescriptionAttribute;
  45. AssemblyCompanyAttribute company = o as AssemblyCompanyAttribute;
  46. AssemblyProductAttribute product = o as AssemblyProductAttribute;
  47. AssemblyCopyrightAttribute copyright = o as AssemblyCopyrightAttribute;
  48. AssemblyTrademarkAttribute trademark = o as AssemblyTrademarkAttribute;
  49. AssemblyVersionAttribute version = o as AssemblyVersionAttribute;
  50. AssemblyFileVersionAttribute fileversion = o as AssemblyFileVersionAttribute;
  51. AssemblyCultureAttribute culture = o as AssemblyCultureAttribute;
  52.  
  53. if (title != null)
  54. sb.AppendFormat("名称: {0}\n", title.Title);
  55. if (desc != null)
  56. sb.AppendFormat("描述: {0}\n", desc.Description);
  57. if (company != null)
  58. sb.AppendFormat("公司: {0}\n", company.Company);
  59. if (product != null)
  60. sb.AppendFormat("产品: {0}\n", product.Product);
  61. if (copyright != null)
  62. sb.AppendFormat("版权: {0}\n", copyright.Copyright);
  63. if (trademark != null)
  64. sb.AppendFormat("商标: {0}\n", trademark.Trademark);
  65. if (version != null)
  66. sb.AppendFormat("程序集版本: {0}\n", version.Version);
  67. if (fileversion != null)
  68. sb.AppendFormat("文件版本: {0}\n", fileversion.Version);
  69. if (culture != null)
  70. sb.AppendFormat("区域性: {0}\n", culture.Culture);
  71. }
  72.  
  73. string GUID = t.GUID.ToString();
  74. bool IsCOMObject = t.IsCOMObject;
  75.  
  76. sb.AppendFormat("GUID: {0}\n", GUID);
  77. sb.AppendFormat("ComVisible: {0}\n", IsCOMObject);
  78.  
  79. return sb.ToString();
  80. }
  81. }
  82. }


运行测试

1111.png

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号