泛型类及方法的基本使用
1、定义
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; } }
2、使用
var ent = new EntBase<EquipQryEnt>(); var lst = ent.GetListDemo();