What is this Type in .NET (Reflection)

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)