刚学反射,这样的该怎么写,给点代码参考下

解决方案 »

  1.   

    public static string GetObjectPropertyValue<T>(T t, string propertyname)
    {
         Type type = typeof(T);      PropertyInfo property = type.GetProperty(propertyname);      if (property == null) return string.Empty;      object o = property.GetValue(t, null);      if (o == null) return string.Empty;      return o.ToString();
    }
    这个是类的,我把他改成
    public static string GetObjectPropertyValue(IList<T> list, string propertyname)
    {
         Type type = list.GetType();      PropertyInfo property = type.GetProperty(propertyname);      if (property == null) return string.Empty;      object o = property.GetValue(t, null);      if (o == null) return string.Empty;      return o.ToString();
    }
    object o = property.GetValue(t, null);里的t我该怎么改
      

  2.   

    Type tt = t.First<T>().GetType();//反射这个,不是反射IList
      

  3.   

    你要反射model中属性的属性值?
    一个model中有多个属性值啊property.GetValue(t, null) 这样想干嘛 反射出什么
      

  4.   

      IList<Model1> lm = new List<Model1>();
                lm.Add(new Model1() { Age = 15, S = 131456, Email = "[email protected]" });
                lm.Add(new Model1() { Name="小李",Age = 25, S = 231456, Email = "[email protected]" });            IList<Dictionary<string, object>> ld = new List<Dictionary<string, object>>();            foreach (Model1 _model in lm)
                {
                    PropertyInfo[] props =_model.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
                    Dictionary<string, object> values = new Dictionary<string, object>();
                    foreach (PropertyInfo property in props)
                    {
                        values.Add(property.Name, property.GetValue(model, property.GetIndexParameters()));
                    }
                    ld.Add(values);
                }  
        public class Model1
        {
            public string Name { get; set; }        public int Age { get; set; }        public int S { get; set; }        public string Email { get; set; }
        }
      

  5.   

    IList<Model1> lm = new List<Model1>();
                lm.Add(new Model1() { Age = 15, S = 131456, Email = "[email protected]" });
                lm.Add(new Model1() { Name="小李",Age = 25, S = 231456, Email = "[email protected]" });            IList<Dictionary<string, object>> ld = new List<Dictionary<string, object>>();            PropertyInfo[] props = typeof(Model1).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
                foreach (Model1 _model in lm)
                {
                    Dictionary<string, object> values = new Dictionary<string, object>();
                    foreach (PropertyInfo property in props)
                    {
                        values.Add(property.Name, property.GetValue(_model, property.GetIndexParameters()));
                    }
                    ld.Add(values);
                }
      

  6.   

    小弟虚心请教,这个是不是获得所有属性的值,我想弄一个函数,获取一个指定的属性值,函数的形式就像这种public static string GetObjectPropertyValue(IList<T> list, string propertyname)
      

  7.   

    小弟虚心请教,这个是不是获得所有属性的值,我想弄一个函数,获取一个指定的属性值,函数的形式就像这种public static string GetObjectPropertyValue(IList<T> list, string propertyname)你一楼写的是什么?  你到底想要啥?
      

  8.   

    加个foreach,然后在foreach内部调用list<T>对象不就行了吗?
      

  9.   

    小弟虚心请教,这个是不是获得所有属性的值,我想弄一个函数,获取一个指定的属性值,函数的形式就像这种public static string GetObjectPropertyValue(IList<T> list, string propertyname)你一楼写的是什么?  你到底想要啥?
    我就是想通过函数,指定list<T>T的属性,然后返回一个string类型的属性值,比如我传入name这个参数,然后返回list里T.name的值
      

  10.   

    foreach(model m in listmodel)
    {
        var s= GetObjectPropertyValue(m,"name");
        //然后你想干嘛就干嘛吧!
    }
      

  11.   

    public static string GetObjectPropertyValue(IList<T> list, string propertyname)
    {
     string str="";
    foreach(T t in list)
    {
         Type type = typeof(T);
     
          PropertyInfo property = type.GetProperty(propertyname);
     
          if (property == null) return string.Empty;
     
          object o = property.GetValue(t, null);
     
          if (o == null) return string.Empty;
     
          str= o.ToString();
    }
    return str;
    }