简体   繁体   English

当我将一个类声明为内部时,为什么IL将其显示为私有?

[英]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: 所以我们有:

  • C#'s internal -> IL's private (to the assembly) C#的internal - > IL的private (对程序集)
  • C#'s private -> IL's nested private (to the enclosing type) C#的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.

相关问题 什么时候应该宣布私人或公共课程? - When should you declare a class private or public? 为什么ProvideAspects方法在实例化属性时不将其添加到IL? - Why does ProvideAspects method not add the attribute to IL when instantiating it? 如果我声明一个内部类,内部成员的默认访问级别是什么? - If I declare an internal class, what the default access level of internal members be? 将“私人”字段声明为私有字段还是内部字段更好? - Is it better to declare a “private” field as private or as internal? C#在类方法中声明私有字符串时,并非所有代码路径都返回值 - C# not all code paths return a value when I declare a private string in a class method 为什么在将C#代码编译成IL时会创建.ctor()? - Why is the .ctor() created when I compile C# code into IL? 与ILMerge或SmartAssembly等工具合并时,内部类是否变为私有? - Does an internal class become private when merged with tools like ILMerge or SmartAssembly? 在C#中,如何将基类声明为私有的? - In C# how do I declare a base class as being private? 为什么可以通过这种方式访问​​私有变量时无法访问基类的私有字段 - Why I can not access private field of a base class when private variables can be accessed this way 为什么IL将此值设置两次? - Why does the IL set this value twice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM