Type objType;
System.Reflection.PropertyInfo prop;
User objUser = new User(); //比如有两个属性 Name (string类型),Age (int类型)
objType = objUser.GetType();prop = objType.GetProperty( "Name" );
prop.SetValue(objUser,"Tom",null);//以上OKprop = objType.GetProperty( "Age" );
prop.SetValue(objUser,2,null);
//转换数字类型时报“不能从目标类型扩展到基元类型”错误。
奇怪ing...

解决方案 »

  1.   

    给你个例子
    using System; 
     
    namespace Test 

        /**//// <summary> 
        /// Class1 的摘要说明。 
        /// </summary> 
        class Class1 
        { 
            /**//// <summary> 
            /// 应用程序的主入口点。 
            /// </summary> 
            [STAThread] 
            static void Main(string[] args) 
            { 
                // 
                // TODO: 在此处添加代码以启动应用程序 
                // 
                MyFieldClass dv=new MyFieldClass(); 
                System.Collections.Hashtable ht1=new System.Collections.Hashtable(); 
                ht1.Add("FieldA","A"); 
                ht1.Add("FieldC","C"); 
                SetField1(ht1,dv);//如果类中的字段匹配Hashtable中的Key则重新设定 
                //SetField2(ht1,dv)//如果Hashtable中的Key匹配类中的字段则重新设定,效果等同于SetField1 
                Console.WriteLine(dv.FieldA);//A 
                Console.WriteLine(dv.FieldB);//bb 
                Console.WriteLine(dv.FieldC);//C 
                System.Collections.Hashtable ht2=new System.Collections.Hashtable(); 
                ht2.Add("PropertyB","b"); 
                ht2.Add("PropertyC","c"); 
                SetProperty1(ht2,dv);//如果类中的属性匹配Hashtable中的Key则重新设定 
                //SetProperty2(ht2,dv);//如果Hashtable中的Key匹配类中的属性则重新设定,效果等同于SetProperty1 
                Console.WriteLine(dv.FieldA);//A 
                Console.WriteLine(dv.FieldB);//b 
                Console.WriteLine(dv.FieldC);//c 
                 
            } 
     
            public static void SetProperty1(System.Collections.Hashtable ht1,MyFieldClass dv) 
            { 
                foreach(System.Collections.DictionaryEntry de in ht1) 
                { 
                    System.Reflection.PropertyInfo pi=dv.GetType().GetProperty(de.Key.ToString()); 
                    if(pi!=null)pi.SetValue(dv,de.Value.ToString(),null); 
                } 
            } 
     
            public static void SetProperty2(System.Collections.Hashtable ht1,MyFieldClass dv) 
            { 
                foreach(System.Reflection.PropertyInfo pi in dv.GetType().GetProperties()) 
                { 
                    if(ht1.Contains(pi.Name))pi.SetValue(dv,ht1[pi.Name],null); 
                } 
            } 
     
            public static void SetField1(System.Collections.Hashtable ht2,MyFieldClass dv) 
            { 
                foreach(System.Collections.DictionaryEntry de in ht2) 
                { 
                    System.Reflection.FieldInfo fi=dv.GetType().GetField(de.Key.ToString()); 
                    if(fi!=null)fi.SetValue(dv,de.Value.ToString()); 
                } 
            } 
     
            public static void SetField2(System.Collections.Hashtable ht2,MyFieldClass dv) 
            { 
                foreach(System.Reflection.FieldInfo fi in dv.GetType().GetFields()) 
                { 
                    if(ht2.Contains(fi.Name))fi.SetValue(dv,ht2[fi.Name]); 
                } 
            } 
        } 
     
        public class MyFieldClass 
        { 
            public string FieldA="aa"; 
            public string FieldB="bb"; 
            public string FieldC="cc"; 
     
            public string PropertyA 
            { 
                get 
                { 
                    return FieldA; 
                } 
                set 
                { 
                    FieldA=value; 
                } 
            } 
     
            public string PropertyB 
            { 
                get 
                { 
                    return FieldB; 
                } 
                set 
                { 
                    FieldB=value; 
                } 
            } 
     
            public string PropertyC 
            { 
                get 
                { 
                    return FieldC; 
                } 
                set 
                { 
                    FieldC=value; 
                } 
            } 
        } 
     

     
      

  2.   

    上面给的例子的属性都是字符串型,这个在我的例子中也可以,但是如果属性是Int 型的,你的
    pi.SetValue(dv,de.Value.ToString(),null) 方法肯定出错。
      

  3.   

    对这种转换,可以用Convert.ChangeType的方法,让方法自动去转换成属性所需要的参数类型
    比如 pi.SetValue(dv,Convert.ChangeType(de.value,pi.PropertyType));
      

  4.   

    修正:我主题贴的例子运行是正确的,
    实际应用中
    prop = objType.GetProperty( "Age" );
    object obj = dr["Age"];//dr["Age"]从数据库取出,debug时为1
    prop.SetValue(objUser,obj,null);
    //这样在转换数字类型时报“不能从目标类型扩展到基元类型”错误。这样的错误好像是属性是数字型,而传入的是字符串时才会有这个错误。
    奇怪的是调试时obj显示的值是1 not "1",应该是对的啊,怎么也会有这个错。
    注,以下两个测试:
    object obj = 1
    prop.SetValue(objUser,obj,null); //okobject obj = "1";
    prop.SetValue(objUser,obj,null); //报“不能从目标类型扩展到基元类型”错误
      

  5.   

    thanks  tijichen(笑起来像狗);