这是什么类型的.NET(反射)反射、类型、这是什么、NET

2023-09-02 12:02:22 作者:你知道小黑么!

这是什么类型的.NET?我使用反射来获取所有的类的列表,这一次轮番上涨。

What is this Type in .NET? I am using reflection to get a list of all the classes and this one turns up.

这是什么?它从何而来?如何名字DisplayClass1选择?我搜索的来源,并没有看到任何东西。什么是<> 是什么意思?什么是ç__ 是什么意思?是有参考?

What is it? where does it come from? How is the name DisplayClass1 chosen? I search the sources and didnt see anything. What does the <> mean? what does the c__ mean? is there reference?

推荐答案

这几乎肯定是由编译器生成的类由于拉姆达EX pression或匿名方法。例如,考虑这个code:

It's almost certainly a class generated by the compiler due to a lambda expression or anonymous method. 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));
    }
}

这是被编译成:

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;
        }
    }
}

...除了使用&LT;&GT; c_displayClass1 的名称,而不是 ExtraClass 。这是一个的难言的名称的,因为它不是有效的C# - 这意味着C#编译器确切地知道它不会出现在自己的code和冲突,其选择

... except using <>c_displayClass1 as the name instead of 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.

编译匿名函数的具体方式是实现特定的,当然 - 这是名称的选择了额外的类

The exact manner of compiling anonymous functions is implementation-specific, of course - as is the choice of name for the extra class.

编译器还产生额外的类迭代器块和(在C#5)异步方法和代表。

The compiler also generates extra classes for iterator blocks and (in C# 5) async methods and delegates.