.NET中的程序集(Assembly)

参考:http://blog.sina.com.cn/s/blog_7ade159d0102wmg9.html

  • 程序集(Assembly,装配件,.NET程序集)
    • 程序集是应用程序的部署单元,.NET应用程序包含一个或多个程序集。
    • 通常编译出来的dll、exe的.NET可执行程序都是程序集
    • .NET程序集包含元数据,这些元数据描述了程序集中定义的所有类型及其成员的信息,即方法、属性、事件和字段。
      • ildasm.exe这种反编译工具用到了?
    • 与命名空间比较
      • 程序集
        • 类库的物理组织形式
        • 通过右击reference添加引用来引入外部库
        • 目的:程序的物理组织,供CLR使用,这些exe、dll中包含一些程序集信息
      • 命名空间
        • 类库的逻辑组织形式
        • 通过using语句来引入外部库
        • 目的:避免产生类名冲突,人类可用的单词数太少,并且不同的人写的程序不可能所有的变量都没有重名现象
      • 只有同时指定类型所在的命名空间及实现该类型的程序集,才能完全限定该类型。
      • 在一个程序集中可以有不同的命名空间,同一个命名空间也可以分布在多个程序集上。命名空间只是类型名的一种扩展,它属于类型名的范畴。
  • 每个project(如果是library,编译后生成一个对应的dll或exe)都会有一些程序集信息(显示在dll或exe的右击属性菜单中)
  • 作用:通过特性(Attribute)来设置程序集(dll文件)的常规信息,供查看或作为配置信息供程序内部使用
  • 对应到VS的项目树中,每个project->Properties->AssemblyInfo.cs文件中都包含了对应的这些程序集信息(一些Attribute)
    • 这个名为AssemblyInfo.cs的文件是vs在.net工程的Properties文件夹下自动生成的,一般情况下我们很少直接改动该文件。
    • using System.Reflection;
      using System.Runtime.CompilerServices;
      using System.Runtime.InteropServices;
      
      // General Information about an assembly is controlled through the following
      // set of attributes. Change these attribute values to modify the information
      // associated with an assembly.
      [assembly: AssemblyTitle("TestWinform")]
      [assembly: AssemblyDescription("")]
      [assembly: AssemblyConfiguration("")]
      [assembly: AssemblyCompany("Licence Owner")]
      [assembly: AssemblyProduct("TestWinform")]
      [assembly: AssemblyCopyright("Copyright © Licence Owner 2018")]
      [assembly: AssemblyTrademark("")]
      [assembly: AssemblyCulture("")]
      
      // Setting ComVisible to false makes the types in this assembly not visible
      // to COM components.  If you need to access a type in this assembly from
      // COM, set the ComVisible attribute to true on that type.
      [assembly: ComVisible(false)]
      
      // The following GUID is for the ID of the typelib if this project is exposed to COM
      [assembly: Guid("1d2f88e5-afc7-468d-a71b-078e27edf595")]
      
      // Version information for an assembly consists of the following four values:
      //
      //      Major Version
      //      Minor Version
      //      Build Number
      //      Revision
      //
      // You can specify all the values or you can default the Build and Revision Numbers
      // by using the '*' as shown below:
      // [assembly: AssemblyVersion("1.0.*")]
      [assembly: AssemblyVersion("1.0.0.0")]
      [assembly: AssemblyFileVersion("1.0.0.0")]
      View Code
  • 我们实际上通过另一个形式操作该AssemblyInfo.cs文件。那就是通过在鼠标右键点击项目的属性进入“应用程序”->“程序集信息”,然后修改信息。
  • 程序中如何读取assembly特性
    • Type t = typeof(Program);

      AssemblyProductAttribute productAttr = t.Assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), true)[0] as AssemblyProductAttribute;

      Console.WriteLine(productAttr.Product);

    • System.Reflection.Assembly.GetExecutingAssembly()
  • 注意
    • [assembly:AssemblyProduct("")] 特性不限于在AssemblyInfo.cs文件中使用,而是可以在任何的.cs文件中使用。
    • 对于同一个特性,程序集中仅能设置一次,否则编译时将报错。
  • 举例
    • [assembly:log4net.Config.XmlConfigurator(Watch=true)] 其实就是配置log4net框架从哪里读配置文件而已,当然这句也可以写到AssemblyInfo.cs文件中统一管理。
原文地址:https://www.cnblogs.com/wyp1988/p/9923454.html