Tuesday, February 17, 2015

Reflection in C#

I was talking with a colleague about reflection, and I started playing with it in LinqPad; for my own reference, here's a small code sample


void Main()
{
Person p=new Person(){Name="Orlando",Age=10};
Console.WriteLine(p.GetType().GetProperties());
Console.WriteLine(p.GetType().GetProperties().Select(x=>x.GetMethod.Invoke(p,null)));
}
// Define other methods and classes here
class Person {
public string Name {get; set;}
public int Age{get;set;}
}