将C#名称空间编译到IL文件中以使其“完整"吗?名字? [英] Is C# namespace compiled into IL files to be "complete" names?
问题描述
例如,如果我有
namespace a
namespace b
{
class C...
class D...
}
因此,编译后,在IL文件中,名称空间信息在哪里?我是否得到两个名为a.b.C和a.b.D的类,其中这些类名以名称空间名称为前缀?
So after compiling, in IL file, where's the namespace information? Do I get two classes named a.b.C and a.b.D where the class names is prefixed by the namespace name?
还是我在汇编文件中得到一个命名空间a.b并在其中包含类C/类D,就像C#代码一样?
Or I get a namespace a.b in the assembly file and having class C/class D inside it, just like C# code?
推荐答案
其他两个响应写了一些东西,所以我必须写相反的内容:-)
The other two responses wrote something, so I have to write the opposite :-)
比方说,微软在这两个阵营中都站稳了脚步……阅读 ECMA-335 :
Let's say that Microsoft kept the foot in both camps... Reading the ECMA-335:
第114页
尽管某些编程语言引入了名称空间的概念,但CLI唯一的支持 这个概念就是作为元数据编码技术.类型名称始终由其全名指定 相对于定义它们的程序集的名称.
While some programming languages introduce the concept of a namespace, the only support in the CLI for this concept is as a metadata encoding technique. Type names are always specified by their full name relative to the assembly in which they are defined.
但是即使是这个ECMA标准,也可以自由使用名称空间概念:
But then even this ECMA standard uses freely the namespace concept:
为了防止将来发生名称冲突,系统名称空间中的所有自定义属性都保留用于标准化.
In order to prevent name collisions into the future, all custom attributes in the System namespace are reserved for standardization.
IL语言支持.namespace
指令,等效于C#的命名空间指令(该指令在ECMA标准中被命名,但是没有示例.ILASM可以正确编译该示例,而反编译的代码就是人们会期望的)...
And the IL language supports the .namespace
instruction, that is equivalent to the namespace instruction of C# (this instruction is named in the ECMA standard but there are no examples. The ILASM compiles correctly this example and the decompiled code is what one would expect)...
.namespace A
{
.namespace B
{
.class public auto ansi beforefieldinit C
extends [mscorlib]System.Object
{
// Nested Types
.class nested public auto ansi beforefieldinit D
extends [mscorlib]System.Object
{
// Methods
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// Method begins at RVA 0x2050
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method D::.ctor
} // end of class D
// Methods
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// Method begins at RVA 0x2050
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method C::.ctor
} // end of class A.B.C
}
}
但是请注意,生成的代码等效于不使用.namespace
并直接在.class
中包括全名:
But note that the generated code is equivalent to not using .namespace
and including directly the full name in the .class
:
.class public auto ansi beforefieldinit A.B.C
extends [mscorlib]System.Object
{
// Nested Types
.class nested public auto ansi beforefieldinit D
extends [mscorlib]System.Object
{
// Methods
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// Method begins at RVA 0x2050
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method D::.ctor
} // end of class D
// Methods
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// Method begins at RVA 0x2050
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method C::.ctor
} // end of class A.B.C
然后Type
类具有Name
和Namespace
属性. Type
类(作为mscorlib的重要部分)对于CLR的良好运行必不可少.
And then the Type
class has a Name
and a Namespace
properties. And the Type
class (as a big piece of mscorlib) is integral to the good working of the CLR.
因此,如果问题是:在已编译的.NET程序中是否存在显式的命名空间?响应为否".只有全名.
So if the question is: is there an explicit namespace in a compiled .NET program? The response is "no". There is only fullnames.
如果问题是:.NET(任何/所有级别)中都有名称空间的概念吗?响应为是".它以IL(源代码级别.namespace
),CLR(.NET API,Type.Namespace
),C#(.NET的主要"语言,用于编写几乎所有.NET库)的形式提供. c9>).
If the question is: is there the concept of namespace in .NET (at any/all levels)? The response is "yes". It is present both in IL (source code level, .namespace
), CLR (.NET API, Type.Namespace
), C# (the "main" language of .NET, used to write nearly all the .NET libraries) (namespace
).
这篇关于将C#名称空间编译到IL文件中以使其“完整"吗?名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!