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);
            }
        }
  • 相关阅读:
    highcharts 时间少8小时问题
    Spring声明式事务配置管理方法
    jetty简介
    java事务管理
    oracle表中某个字段含有字符回车、空格的手动修改方式
    java环境变量配置
    JAVA解析XML的四种方式
    JSON-lib框架,JAVA对象与JSON、XML之间的相互转换
    Java WebService简单实例
    HTTP协议报文、工作原理及Java中的HTTP通信技术详解
  • 原文地址:https://www.cnblogs.com/stanley107/p/2549216.html
Copyright © 2011-2022 走看看