泛型类及方法的基本使用

时间:2014-05-22 03:35:38   收藏:0   阅读:166

1、定义

bubuko.com,布布扣
 public class EntBase<T>:where  T: new()
    {
        public List<T> GetListDemo()
        {
            var lst = new List<T>();
            for (int i = 1; i <= 35; i++)
            {
                var ent = new T();
                foreach (var item in ent.GetType().GetProperties())
                {
                    var v = (DescriptionAttribute[])item.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    var descriptionName = v[0].Description;

                    item.SetValue(ent, descriptionName + ":" + i.ToString(), null);
                }

                lst.Add(ent);
            }
            return lst;
        }

        public string GetJsonDemo<T>()
        {
            var oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();  //命名空间为:System.Web.Extensions
            string sJSON = oSerializer.Serialize(GetListDemo<T>());
            return sJSON;
        }
    }
bubuko.com,布布扣

 

2、使用

 

   var ent = new EntBase<EquipQryEnt>();
            var lst = ent.GetListDemo();

 

泛型类及方法的基本使用,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!