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);
            }
        }
  • 相关阅读:
    Python 爬虫的工具列表
    使用rabbitmq手动确认消息的,定时获取队列消息实现
    redis订阅发布简单实现
    ubuntu下打开html页面
    关系数据库基本术语
    事务的基本概念,附图示
    oracle 一对多数据分页查询筛选
    一个在linux环境执行io操作的bug
    再springMVC中自定义文件上传处理解决与原spring中MultipartResolve冲突问题
    oracle存储过程删除树状结构的表数据
  • 原文地址:https://www.cnblogs.com/stanley107/p/2549216.html
Copyright © 2011-2022 走看看