C# 3.0 : Anonymous Type and .NET Reflection Hand-in-Hand

Anonymous Type is nothing but the type which gets defined by CLR (not you). So that type is as rich as any other normal type. Probably the conventional way to find the components of an Anonymous Type through reflection would be clearer to us,

 

static void Main()

{

    //Anonymous Type

    var anyType = new

    {

        IntID = 1,

        StringName = "Wriju"

    };

    Type t = anyType.GetType();

    PropertyInfo[] pi = t.GetProperties();

    foreach (PropertyInfo p in pi)

    {

        //Get the name of the prperty

        Console.WriteLine(p.Name);

    }

    //Using LINQ get all the details of Property

    var query = from p in t.GetProperties()

                select p;

    ObjectDumper.Write(query);

}

 

 

 

Namoskar!!!