How to Get the Name of the Anonymous class(type) in C# ?

Below is a sample code snippet which gets the name of the anonymous class . In the below example , the Name and the ID property is set which is anonymous where the type is not specified. When trying to find out the type of the anonymous type , you will get the type as shown in the screenshot below.

How to Get the Name of the Anonymous class in C# ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ACSample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the name of the anonymous class
            var employee = new { Name = "Martin", ID = 1 };
            Console.WriteLine(employee.GetType().ToString());
            Console.ReadLine();
        }
    }
}

1