public class Testcls
        {
            public string strName { get; set; }
        }
Testcls itm = new Testcls();itm.GetType().InvokeMember("strName", System.Reflection.BindingFlags.SetProperty, null, itm, new object[] { 22 });
始终报告  未找到方法
什么问题,怎么解决?反射

解决方案 »

  1.   

    itm.GetType().GetProperty("strName").SetValue(itm, "22");
      

  2.   

    你的strName是string类型,改成itm.GetType().InvokeMember("strName", System.Reflection.BindingFlags.SetProperty, null, itm, new object[] { "22" });
    应该就可以了。
      

  3.   

    itm.GetType().InvokeMember("strName", System.Reflection.BindingFlags.SetProperty, null, itm, new object[] { "22" }); 依然报告找不到方法
    itm.GetType().GetProperty("strName").SetValue(itm, "22"); 
    这个方法行不通,strName 是举例的,实际类型不确定
      

  4.   

                Testcls itm = new Testcls();
                object obj = new object();
                obj = 22;
                itm.GetType().GetProperty("strName").SetValue(itm, obj, null);这样,直接就报错了我要从别的地方读取值,用foreach (PropertyInfo p in itm.GetProperties())放到属性里面,没法确定属性的类型的
      

  5.   

    是System.Reflection.BindingFlags.SetProperty不是System.Reflection.BindingFlags.SetField
      

  6.   

    解决问题不是出在代码上是在excel文件中的,要把excel文件中对应的单元格属性改为相应的文本和数值类型,否则就在SetProperty的时候报告找不到方法猜测属性定义会在编译后生成方法,excle中不改类型,读取后,调用时会因为参数不对而报告找不到对应的方法。