代码改变世界

泛型约束通过反射获取相关值、属性名、属性类型

2014-06-09 14:32  hf_sun  阅读(503)  评论(0编辑  收藏  举报

public class SpliceSqlWhere<T>
    {

          public static string GetSqlWhere(T t)

      {

               PropertyInfo[] pInfo = t.GetType().GetProperties();
                 List list = new List();
                 for (int i = 0; i < pInfo.Length; i++)
                 {
                     ObjectParameter para = new ObjectParameter();
                     para.Type = pInfo[i].PropertyType.Name;
                     para.Name = pInfo[i].Name;
                     para.Value = pInfo[i].GetValue(t);
                     list.Add(para);
                 }

      }

}

 

 class ObjectParameter
    {
        public string Type { get; set; }
        public string Name { get; set; }
        public object Value { get; set; }
    }

分享: