What is this Type in .NET (Reflection)

后端 未结 2 1703
鱼传尺愫
鱼传尺愫 2020-12-29 20:11

What is this Type in .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 Display

相关标签:
2条回答
  • 2020-12-29 20:17

    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'

    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.

    0 讨论(0)
  • 2020-12-29 20:24

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

    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. 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.

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

    0 讨论(0)
提交回复
热议问题