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);
            }
        }
  • 相关阅读:
    mybatis入参错误:There is no getter for property named ‘status‘ in ‘class java.lang.Integer‘
    JAVA程序员面试笔试题(一)
    Java8新特性LocalDateTime获取周几
    linux常用命令记录 screen
    ubuntu 19.04 + lenovo-xiaoxin-I2000 触摸板右键单击无法使用
    华为交换路由常用命令
    centos7常用软件
    一般网络延迟高的原因
    华为防火墙进程&简单配置
    私网互联(本质是三层路由)
  • 原文地址:https://www.cnblogs.com/stanley107/p/2549216.html
Copyright © 2011-2022 走看看