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);
            }
        }
  • 相关阅读:
    【BZOJ】2729: [HNOI2012]排队
    【BZOJ】2734: [HNOI2012]集合选数
    【BZOJ】3573: [Hnoi2014]米特运输
    【BZOJ】4008: [HNOI2015]亚瑟王
    【BZOJ】[HNOI2015]菜肴制作
    数论六·模线性方程组
    数论五·欧拉函数
    黄金矿工
    数论四·扩展欧几里德
    数论三·约瑟夫问题
  • 原文地址:https://www.cnblogs.com/stanley107/p/2549216.html
Copyright © 2011-2022 走看看