zoukankan      html  css  js  c++  java
  • 利用反射来实现类(含可空字段)的映射

       public override void MapToEntity(object obj)
            {
                DBSettleAccountWithWaitingSummary summary =( DBSettleAccountWithWaitingSummary)obj;
                Type entityType = this.GetType();
                PropertyInfo[] ePropertyInfos = entityType.GetProperties();
                string propertyName;
                foreach (PropertyInfo ePropertyInfo in ePropertyInfos)
                {
                    propertyName=ePropertyInfo.Name;
                    if (propertyName.Equals("GetAction") || propertyName.Equals("DisplayText")) continue;
                    PropertyInfo dbPropertyInfo = summary.GetType().GetProperty(propertyName);
                    ePropertyInfo.SetValue(this, dbPropertyInfo.GetValue(summary, null).ChangeType(ePropertyInfo.PropertyType), null);
                }

            }
    public static class ZBConvert
        {
            public static object ChangeType(this object value, Type conversionType)
            {
                if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                {
                    if (value != null)
                    {
                        NullableConverter nullableConverter = new NullableConverter(conversionType);
                        conversionType = nullableConverter.UnderlyingType;
                    }
                    else
                    {
                        return null;
                    }
                }

                return Convert.ChangeType(value, conversionType);
            }
        }
  • 相关阅读:
    Schema和数据类型优化?
    语雀发布博客园
    为知笔记文章目录
    码云搭建博客
    springboot的过滤器、监听器、拦截器
    springboot常用注解
    springboot使用小技巧合集
    springboot整合swagger2
    强制卸载win软件
    xshell下载和优化配置
  • 原文地址:https://www.cnblogs.com/stanley107/p/2549216.html
Copyright © 2011-2022 走看看