问题1:我现在给出一个字符串比如"Employee",我想通过C#用这个字符串生成一个类Employee,(Employee类已经存在)问题2: 在我生成类的基础上我在给一个字符串比如".Age",我想通过C# 得到Employee类的Age属性即Employee.Age

解决方案 »

  1.   

    使用codedom动态编译,搜一下,很多的教程
      

  2.   

    http://msdn2.microsoft.com/zh-cn/library/f1dfsbhc(vs.80).aspx
    http://lichdr.cnblogs.com/category/12610.html
      

  3.   

    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();
      

  4.   

    用reflect完全能实现 不过比较麻烦 强类型语言这样做是比较繁琐的
    网上很多 可以搜一下
      

  5.   

    被误导了...
    1.string em = "Employee";
      Employee employee = (Employee)System.Activator.CreateInstance(typeof(Employee),em);
      

  6.   

    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);