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

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

  • Comments 2

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

 

Leave a Comment
  • Please add 7 and 4 and type the answer here:
  • Post
Page 1 of 1 (2 items)