首页 > 代码库 > 反射用法

反射用法

static void Main(string[] args)       
{ var retul = GetListData<User>("wang");
}
public static List<T> GetListData<T>(string name) where T:class,new()
{
    List<string> ls = new List<string>() {"wang","li","zhao" };
    PropertyInfo[] pros = typeof(T).GetProperties();
    List<T> result = new List<T>();
    if(ls.Contains(name))
    {
      var bb= ls.Where(p=>p =="wang").FirstOrDefault();
      T t = new T();
      pros[0].SetValue(t,Convert.ChangeType(bb,pros[0].PropertyType),null);
      result.Add(t);
    }
    return result;
  }