[英]When I declare a class as internal, why does the IL show it as private?
If I declare a class as internal, why does the IL show it as private? 如果我将一个类声明为内部类,为什么IL将其显示为私有?
internal class Thing
.class private auto ansi beforefieldinit Thing.Thing
extends [mscorlib]System.Object
From IL's point of view, private
means private to the assembly , ie internal
in C#. 从IL的角度来看,
private
意味着组装私有 ,即C# internal
。
In C#, it is not possible to mark types as private
if they are not nested . 在C#中,如果未嵌套 ,则无法将类型标记为
private
。 IL's equivalent accessibility for such types is nested private
. IL对这些类型的等效可访问性是
nested private
。
So we have: 所以我们有:
internal
-> IL's private
(to the assembly) internal
- > IL的private
(对程序集) private
-> IL's nested private
(to the enclosing type) private
- > IL的nested private
(对于封闭类型) On MSDN , it says: 在MSDN上 ,它说:
The C# keywords protected and internal have no meaning in IL and are not used in the reflection APIs.
C#关键字protected和internal在IL中没有任何意义,并且不在反射API中使用。 The corresponding terms in IL are Family and Assembly.
IL中的相应术语是Family和Assembly。 To identify an internal method using reflection, use the IsAssembly property.
要使用反射标识内部方法,请使用IsAssembly属性。 To identify a protected internal method, use the IsFamilyOrAssembly.
要标识受保护的内部方法,请使用IsFamilyOrAssembly。
So I guess it just makes them private, since they can't be accessed from elsewhere. 所以我猜它只是让它们变成私有的,因为它们无法从其他地方访问。
EDIT: I see how my answer might not be completely correct, I just thought it was something worth noting. 编辑:我看到我的答案可能不完全正确,我只是觉得这是值得注意的事情。 The MSDN article I linked has some interesting stuff on this "what code we write" - "what it becomes" relationship.
我链接的MSDN文章在这个“我们编写的代码” - “它变成什么样”的关系中有一些有趣的东西。
The mapping of C# keywords to IL keywords isn't always a logical one. C#关键字到IL关键字的映射并不总是合乎逻辑的。 Ecma-335, section II.23.1.15 shows what flags are valid for a type.
Ecma-335,第II.23.1.15节显示了哪种标志对一种类型有效。 You'll see that it only defines Public, NotPublic and a set of NestedXxx flags.
你会看到它只定义了Public,NotPublic和一组NestedXxx标志。 Nothing similar to "internal".
没有类似于“内部”的东西。 So your class is actually NotPublic, displayed as "private" in ildasm.
所以你的班级实际上是NotPublic,在ildasm中显示为“私人”。
It is easy to see a side-effect of this: try this declaration in your C# code: 很容易看到这样的副作用:在C#代码中尝试此声明:
private class DoesNotWork {
}
You'll get: 你会得到:
error CS1527: Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal
错误CS1527:命名空间中定义的元素不能显式声明为private,protected或protected internal
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.