解决方案 »

  1.   

    谁告诉你必须是static标记啊,反射有参数可以设置,是否取static的还是取其他类型的
      

  2.   

    难道楼主要的不是?p.SetValue(result, XXX);
      

  3.   


    p.SetValue(result, XXX);  等价于 result = XXX吧?这里我是要设置设置当前someClass的str2的值为 “123” 怎么设?p.SetValue(p, “123”); ?
      

  4.   


    在我写的代码里如果不标记static,#region 測試資料轉換
        /// <summary>
        /// 測試資料轉換
        /// </summary>
        public class someClass
        {
            public static string str1 { get; set; }
           public string str2 { get; set; }
            public int int1 { get; set; }
            public int int2 { get; set; }
            public static DateTime dt1 { get; set; }
            public DateTime dt2 { get; set; }
            public bool b1 { get; set; }
            public bool b2 { get; set; }
        }
        #endregion
    方法p.SetValue(null, "132") 会提示错误:非静态方法需要目标
     foreach (System.Reflection.PropertyInfo p in result.GetType().GetProperties())
                {
                    if ((p.PropertyType.Name == "String"))
                    {
                        p.SetValue(null, "132");
                    }
    }::p.SetValue(null, "132"); 第一个参数是应该写什么?
    p.SetValue(p, "132")这样写提示对象和目标类型不匹配,
    写成p.SetValue(p.PropertyType.Name, "132");仍是提示与目标类型不匹配、、、、、、刚试了二楼的,可以了