简体   繁体   English

.NET(反射)中的此类型是什么

[英]What is this Type in .NET (Reflection)

What is this Type in .NET? .NET中的此类型是什么? I am using reflection to get a list of all the classes and this one turns up. 我正在使用反射来获取所有类的列表,而这一类又出现了。

What is it? 它是什么? where does it come from? 它从何而来? How is the name DisplayClass1 chosen? 如何选择DisplayClass1名称? I search the sources and didnt see anything. 我搜索了资料来源,却没有看到任何东西。 What does the <> mean? <>是什么意思? what does the c__ mean? c__是什么意思? is there reference? 有参考吗?

在此处输入图片说明

It's almost certainly a class generated by the compiler due to a lambda expression or anonymous method. 由于lambda表达式或匿名方法,几乎​​可以肯定这是由编译器生成的类。 For example, consider this code: 例如,考虑以下代码:

using System;

class Test
{
    static void Main()
    {
        int x = 10;
        Func<int, int> foo = y => y + x;
        Console.WriteLine(foo(x));
    }
}

That gets compiled into: 编译成:

using System;

class Test
{
    static void Main()
    {
        ExtraClass extra = new ExtraClass();
        extra.x = 10;

        Func<int, int> foo = extra.DelegateMethod;
        Console.WriteLine(foo(x));
    }

    private class ExtraClass
    {
        public int x;

        public int DelegateMethod(int y)
        {
            return y + x;
        }
    }
}

... except using <>c_displayClass1 as the name instead of ExtraClass . ...除了使用<>c_displayClass1作为名称而不是ExtraClass This is an unspeakable name in that it isn't valid C# - which means the C# compiler knows for sure that it won't appear in your own code and clash with its choice. 这是一个难以置信的名称 ,因为它不是有效的C#-这意味着C#编译器肯定会知道它不会出现在您自己的代码中并与它的选择相冲突。

The exact manner of compiling anonymous functions is implementation-specific, of course - as is the choice of name for the extra class. 当然,编译匿名函数的确切方式是特定于实现的-额外类的名称选择也是如此。

The compiler also generates extra classes for iterator blocks and (in C# 5) async methods and delegates. 编译器还会为迭代器块以及(在C#5中)异步方法和委托生成额外的类。

Jon is of course correct. 乔恩当然是正确的。 I've provided a "decoder ring" for figuring out what the various compiler-generate type names mean here: 我提供了一个“解码器环”,用于弄清各种编译器生成的类型名在这里的含义:

Where to learn about VS debugger 'magic names' 在哪里了解VS调试器的“魔术名称”

The names are quite long and we sometimes get complaints that we're bulking up the size of metadata as a result. 名称很长,有时我们会抱怨说我们增加了元数据的大小。 We might change the name generation rules to address this concern at any time in the future. 我们可能会在将来的任何时候更改名称生成规则以解决此问题。 It is therefore very important that you not write code that takes advantage of knowledge of this compiler implementation detail. 因此,不要编写利用此编译器实现细节知识的代码非常重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM