Having a “+” in the class name?

你。 提交于 2019-12-28 04:13:06

问题


Class name: MyAssembly.MyClass+MyOtherClass

The problem is obviously the + as separator, instead of traditionnal dot, its function, and to find official documentation to see if others separators exist.


回答1:


That's just the way that a nested type is represented. So for example:

namespace Foo
{
    class Outer
    {
        class Nested {}
    }
}

will create a type with a full name of Foo.Outer+Nested in the compiled code. (So that's what typeof(Outer.Nested).FullName would return, for example.)

It's not clear to me whether this is specified behaviour, or just what the Microsoft C# compiler chooses to use; it's an "unspeakable" name in that you couldn't explicitly declare a class with a + in it in normal C#, so the compiler knows it won't clash with anything else. Section 10.3.8 of the C# 3 spec doesn't dictate the compiled name as far as I can see.

EDIT: I've just seen that Type.AssemblyQualifiedName specifies that "+" is used to precede a nested type name... but it's still not clear whether or not that's actually required or just conventional.




回答2:


This is what the compiler uses in the metadata to represent a nested class.

i.e.

class A { class B {} }

would be seen as

class A+B

in the metadata



来源:https://stackoverflow.com/questions/2443244/having-a-in-the-class-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!