object obj = System.Activator.CreateInstance(Type.GetType("ReflectCreateClassSetPropertyTest.Employee")); Type t = obj.GetType(); System.Reflection.PropertyInfo[] pis = t.GetProperties(); for(int i=0; i<pis.Length; i++) { if(pis[i].Name == "Age") { pis[i].SetValue(obj, "20", null); } } // 测试代码: Employee e = obj as Employee; Console.WriteLine(e.Age); Console.Read();
用reflect完全能实现 不过比较麻烦 强类型语言这样做是比较繁琐的 网上很多 可以搜一下
被误导了... 1.string em = "Employee"; Employee employee = (Employee)System.Activator.CreateInstance(typeof(Employee),em);
object obj = System.Activator.CreateInstance(Type.GetType("ReflectCreateClassSetPropertyTest.Employee")); Type t = obj.GetType(); System.Reflection.PropertyInfo pi = t.GetProperty("Age"); pi.SetValue(obj, "20", null); string age = pi.GetValue(obj, null);
http://lichdr.cnblogs.com/category/12610.html
Type t = obj.GetType();
System.Reflection.PropertyInfo[] pis = t.GetProperties();
for(int i=0; i<pis.Length; i++)
{
if(pis[i].Name == "Age")
{
pis[i].SetValue(obj, "20", null);
}
} // 测试代码:
Employee e = obj as Employee;
Console.WriteLine(e.Age);
Console.Read();
网上很多 可以搜一下
1.string em = "Employee";
Employee employee = (Employee)System.Activator.CreateInstance(typeof(Employee),em);
Type t = obj.GetType();
System.Reflection.PropertyInfo pi = t.GetProperty("Age");
pi.SetValue(obj, "20", null);
string age = pi.GetValue(obj, null);