添加一个方法方便映射

用法

        public async Task<List<PayInfoDto>> TestMapMulti2One()
        {


            var result = from variety in _varietyRepository.GetAll()
                         join product in _productRepository.GetAll() on variety.Id equals product.BasBloodVarietyId
                         select Dynamic.MapMulti2One<PayInfoDto>(variety, product);

            return result.ToList();
        }

实体类

    public class PayInfoDto
    {
        public string Name { get; set; }
        //这个字段 取 BasBloodProduct表的 Name字段
        [MapAttribute("BasBloodProduct", "Name")]
        public string ProductName { get; set; }
    }

特性方法

    public class OtherMapMult
    {
        public string TableName { get; set; }
        public string SetName { get; set; }
        public string GetName { get; set; }
    }
    public class MapAttribute : Attribute
    {
        private string _tableName { get; set; }
        private string _setName { get; set; }

        public MapAttribute(string tableName,string setName)
        {
            _tableName = tableName;
            _setName = setName;
        }
        public OtherMapMult MapValidate()
        {

            return new OtherMapMult {
                SetName=_setName,
                TableName=_tableName,
            };

        }
    }

方便映射的公共方法 

        //把入参的属性赋值给出参
        public static T MapMulti2One<T>(params object[] arrObj) where T : new()
        {
            //检查是否存在特性
           // 使用反射获取A和B对象的属性信息 要映射的对象 Dto
                var propertiesT = typeof(T).GetProperties();

                List<OtherMapMult> list = new List<OtherMapMult>();
                foreach (var prop in propertiesT)
                {

                    //查看属性是否具有 MapAttribute特性
                    if (prop.IsDefined(typeof(MapAttribute), true))
                    {
                        foreach (MapAttribute itemMapAttribute in prop.GetCustomAttributes(typeof(MapAttribute), true))//获取字段所有的特性
                        {
                            var itemMap = itemMapAttribute.MapValidate();
                            if (itemMap!=null)
                            {
                                itemMap.GetName=prop.Name;
                                list.Add(itemMap);
                            }
                        }
                    }
                }
            //遍历数据映射
            var t = new T();
            foreach (var item in arrObj)
            {
                if (item==null) {
                    continue;
                }
                var propertiesA = item.GetType().GetProperties();

                foreach (var propertyA in propertiesA)
                {
                    var teGetVuale = list.FirstOrDefault(ta => ta.SetName == propertyA.Name && ta.TableName == propertyA.DeclaringType.Name);

                    var propertyT = propertiesT
                        .FirstOrDefault(p => p.Name == (teGetVuale?.GetName ?? propertyA.Name) && p.PropertyType == propertyA.PropertyType);

                    if (propertyT != null)
                    {
                        var value = propertyA.GetValue(item);
                        var convertedValue = Convert.ChangeType(value, propertyT.PropertyType);
                        propertyT.SetValue(t, convertedValue);
                    }
                }
            }

            return t;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值