日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

泛型对象实例化

發布時間:2025/6/17 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 泛型对象实例化 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

繼承new()這個就可以進行泛型實例化了。本來還想著直接傳一個進去(ActivityPlayRequest request,T model, ActivityDbContext dbContext)

但是由于list引用的是地址指針,所以到最后List中的所有數據都是model最后一次的改變結果,比較坑。

所以查了一下泛型實例化的寫法。當然這種寫法要求該對象存在無參的構造函數才行。

是從這里參考的:Author ※森林小居※

http://www.cnblogs.com/Slxj/archive/2011/10/13/2210443.html

?

public List<T> GetAbstractUserDTO<T>(ActivityPlayRequest request,ActivityDbContext dbContext) where T : AbstractUserDTO,new(){//temp用來存放一個值,因為T泛型不能創建var merchant = dbContext.Find<Merchant>(request.MerchantID);if (merchant == null){return null;}var result = new List<T>();var weixinUserList = dbContext.WeixinUsers.ToList();var merchantWeixinUserList = dbContext.MerchantWeixinUsers.ToList();if (!string.IsNullOrEmpty(request.Keyword)){request.Keyword = request.Keyword.Trim();}//參加用戶的唯一表#region 存儲值var playList = dbContext.ActivityScenePlayRecords.Where(u => u.ActivitySceneID == request.ActivitySceneID).ToList();foreach (var item in playList){var temp = new T();temp.MerchantWeixinUserID = item.MerchantWeixinUserID;temp.WeixinUserID = item.WeixinUserID;#region 真實姓名聯系方式,不通過weixinuser表了,而是使用ActivityScenePlayRecord表進行獲取temp.RealName = item.RealName;temp.TelPhone = item.Telphone;#endregionvar merchantWeixinUser = merchantWeixinUserList.Where(u => u.ID == item.MerchantWeixinUserID).FirstOrDefault();temp.NickName = merchantWeixinUser.NickName;temp.OpenId = merchantWeixinUser.OpenId;temp.ImageUrl = merchantWeixinUser.HeadImageUrl;#region 根據keyword判斷是否要添加進入if (!string.IsNullOrEmpty(request.Keyword)){if (!string.IsNullOrEmpty(temp.RealName) && temp.RealName.Contains(request.Keyword)){result.Add(temp);}else if (!string.IsNullOrEmpty(temp.TelPhone) && temp.TelPhone.Contains(request.Keyword)){result.Add(temp);}else if (!string.IsNullOrEmpty(temp.NickName) && temp.NickName.Contains(request.Keyword)){result.Add(temp);}}else{result.Add(temp);}#endregion}#endregionreturn result;}

?

轉載于:https://www.cnblogs.com/danlis/p/5359372.html

總結

以上是生活随笔為你收集整理的泛型对象实例化的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。