AssemblyInfo.cs文件详解-飞外

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq395537505/article/details/49661555

一、前言

.net工程的Properties文件夹下自动生成一个名为AssemblyInfo.cs的文件,一般情况下我们很少直接改动该文件。但我们实际上通过另一个形式操作该文件。那就是通过在鼠标右键点击项目的属性进入 应用程序 - 程序集信息 ,然后修改信息。

二、作用

通过特性(Attribute)来设置程序集(dll文件)的常规信息,供查看或作为配置信息供程序内部使用。

三、详解


对生成的dll文件右键点击属性,即可查看到上述信息。

assembly:表示特性以程序集作为作用范围。

四、读取assembly特性

以读取[assembly:AssemblyProduct("")]为例子

Type t = typeof(Program);AssemblyProductAttribute productAttr = t.Assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), true)[0] as AssemblyProductAttribute;Console.WriteLine(productAttr.Product);

五、注意点

1.[assembly:AssemblyProduct("")]特性不限于在AssemblyInfo.cs文件中使用,而是可以在任何的.cs文件中使用。

2. 对于同一个特性,程序集中仅能设置一次,否则编译时将报错。

示例:

[assembly:AssemblyProduct("")]namespace{ public class Demo{}

六、总结

现在回头看《.Net魔法堂:log4net详解》当中的那句[assembly:log4net.Config.XmlConfigurator(Watch=true)]应该就清晰多了。其实就是配置log4net框架从哪里读配置文件而已,当然这句也可以写到AssemblyInfo.cs文件中统一管理咯!

尊重原创,转载请注明来自:http://blog.csdn.net/qq395537505点击打开链接

七、参考

http://www.itwis.com/html/net/aspnet/20091211/7123.html