zoukankan      html  css  js  c++  java
  • 泛型对象实例化

    继承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;
                        #endregion
                            var 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
                    }
                    #endregion
                    return result;
            }
    复制代码
    https://www.cnblogs.com/danlis/p/5359372.html
  • 相关阅读:
    【2012百度之星资格赛】G:聊天就是Repeat
    使用testNG进行并发性能测试
    关于处理高并发,防止库存超卖的问题
    关于java内存溢出的异常处理
    关于微服务架构
    关于MySQL绿色版的安装
    G点营销看来也不是@z营销的专利啊
    matlab+中文字体设计,有搞头没有? 有搞头
    字王字型系列命名草案
    国外字体设计师也是蛮重视数学的
  • 原文地址:https://www.cnblogs.com/hedianzhan/p/8995410.html
Copyright © 2011-2022 走看看