Open hnscdg opened 5 years ago
命名空间与程序集的区别。
命名空间用于对类型进行逻辑分组。程序集则是程序的物理分组,对应于一个dll或exe文件
为什么要使用两种方式分组?把逻辑分组作为物理分组不是更简单吗?
逻辑分组主要针对开发人员,而物理分组主要针对安装和部署。
除此之外,以下情况也需要使用两种方式分组,否则会很不方便。
团队开发。一个公司分别由不同的部门共同开发同一个命名空间的不同组件。那么不同的部门可以把他们开发的东西编译成各自的dll文件。使用时把几个dll一起引用即可。
一个命名空间里包含的类型太多,而有大部分是用不上的。如果把一个命名空间编译成一个dll,则会降低运行效率。
有时需要对一个命名空间内的类型区别对待。如果打算开发一个系列软件,该软件分普通版,专业版,高级版。越高的版本具有越高的权限(当中包含了一些高级的类),这时可以把高级的功能放到一个单独的dll来控制。
类型可以没有命名空间吗?
可以
Object类的名字到底是Object还是System.Object?
可以说都是也可以说都不是。
对于IL来说,确定一个类型是需要程序集+类型全名称的。比方调用Object的ToString()方法,其实是[MSCorLib.dll]System.Object::ToString()。因此CLR不知道命名空间的事情,它只知道调用MSCorLib.dll中的System.Object类。
对于C#来说,编译后会自动把命名空间加上点号让类型名变成全名称。但是,类名是不能出现点(.)符号的,以下代码在逻辑上是成立的,但是C#语法不允许。
命名空间和程序集的关系:
从属于一个命名空间的各个类型可能在不同的程序集中实现。例如:system.IO.FileStream类型是在MSCorlib.dll 程序集中实现的,而System.IO.FileSystemWatcher类型是在System.dll 程序集中实现的。事实上, .Net framework 甚至根本没有发布一个System.IO.dll程序集。(一个程序集内可能有多个命名空间,同一个命名空间可能存在于不同的程序集中.)
在一个程序集中,也可能包含不同命名空间中的类型。例如,System.Int32和System.Text.StringBuilder类型都在MSCorLib.dll程序集中。
在.net framework SDk文档中查找一个类型时,文档会明确的指出类型所属的命名空间,以及实现了该类型的程序集。
命名空间与程序集的区别。
命名空间用于对类型进行逻辑分组。程序集则是程序的物理分组,对应于一个dll或exe文件
为什么要使用两种方式分组?把逻辑分组作为物理分组不是更简单吗?
逻辑分组主要针对开发人员,而物理分组主要针对安装和部署。
除此之外,以下情况也需要使用两种方式分组,否则会很不方便。
团队开发。一个公司分别由不同的部门共同开发同一个命名空间的不同组件。那么不同的部门可以把他们开发的东西编译成各自的dll文件。使用时把几个dll一起引用即可。
一个命名空间里包含的类型太多,而有大部分是用不上的。如果把一个命名空间编译成一个dll,则会降低运行效率。
有时需要对一个命名空间内的类型区别对待。如果打算开发一个系列软件,该软件分普通版,专业版,高级版。越高的版本具有越高的权限(当中包含了一些高级的类),这时可以把高级的功能放到一个单独的dll来控制。
类型可以没有命名空间吗?
可以
Object类的名字到底是Object还是System.Object?
可以说都是也可以说都不是。
对于IL来说,确定一个类型是需要程序集+类型全名称的。比方调用Object的ToString()方法,其实是[MSCorLib.dll]System.Object::ToString()。因此CLR不知道命名空间的事情,它只知道调用MSCorLib.dll中的System.Object类。
对于C#来说,编译后会自动把命名空间加上点号让类型名变成全名称。但是,类名是不能出现点(.)符号的,以下代码在逻辑上是成立的,但是C#语法不允许。
命名空间和程序集的关系:
从属于一个命名空间的各个类型可能在不同的程序集中实现。例如:system.IO.FileStream类型是在MSCorlib.dll 程序集中实现的,而System.IO.FileSystemWatcher类型是在System.dll 程序集中实现的。事实上, .Net framework 甚至根本没有发布一个System.IO.dll程序集。(一个程序集内可能有多个命名空间,同一个命名空间可能存在于不同的程序集中.)
在一个程序集中,也可能包含不同命名空间中的类型。例如,System.Int32和System.Text.StringBuilder类型都在MSCorLib.dll程序集中。
在.net framework SDk文档中查找一个类型时,文档会明确的指出类型所属的命名空间,以及实现了该类型的程序集。