Generic object instantiation

Inheriting new() allows for generic instantiation. I originally thought of passing one directly (ActivityPlayRequest request, T model, ActivityDbContext dbContext)

But because the list refers to the address pointer, all the data in the last list is the result of the last change of the model, which is quite pitiful.

So check the writing of generic instantiation. Of course, this way of writing requires the object to have a no-argument constructor.

Referenced from here: Author  ※Forest House※

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

 

copy code
public List<T> GetAbstractUserDTO<T>(ActivityPlayRequest request,ActivityDbContext dbContext) where T : AbstractUserDTO,new()
        {
            //temp is used to store a value, because T generics cannot be created
                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();
                }
                // Unique table for participating users
                #region store value
                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 The real name and contact information, not through the weixinuser table, but obtained using the ActivityScenePlayRecord table
                    temp.RealName = item.RealName;
                    temp.TelPhone = item.Telphone;
                    #endregion
                        var merchantWeixinUser = merchantWeixinUserList.Where(u => u.ID == item.MerchantWeixinUserID).FirstOrDefault();
                        temp.NickName = merchantWeixinUser.NickName;
                        temp.OpenId = merchantWeixinUser.OpenId;
                        temp.ImageUrl = merchantWeixinUser.HeadImageUrl;
                  
                    #region Determine whether to add entry according to the 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
                }
                #endregion
                return result;
        }
copy code
https://www.cnblogs.com/danlis/p/5359372.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325384214&siteId=291194637