Say I have a class
public class Employee
{
public string name = "";
public int age = 20;
}
now I want a function that can dynamically get the value of a specified member of Employee
public class Util<T>
{
public static string GetValue(T obj, string member)
{
// how to write
}
}
so that I can use it like
Employee p1 = new Employee();
string a = Util<Employee>.GetValue(p1, "age"); // a should be "20"
How to do it? like access member use obj["???"] in PHP
Reflection. Take a look here:
Reflection: Property Value by Name
Oh, and also here in our very own SO!
Update
Sigh... It's all about "teh codez"... Here you go:
p1.GetType().GetProperty("age").GetValue(p1,null);
But do check the links, there's stuff to learn!