码迷,mamicode.com
首页 > 其他好文 > 详细

对象Transform,对属性赋值

时间:2014-08-12 12:47:04      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:io   for   ar   cti   new   type   ef   on   

private void ContructRequest(Dictionary<string, string> dictionary, CustomerSearchRequest request)
        {
            for (int i = 0; i < dictionary.Count; i++)
            {
                var property = request.GetType().GetProperties().FirstOrDefault(p => p.Name == dictionary.Keys.ElementAt<string>(i));
                if (property == null) continue;
                var propertyValue = ConvertToPropType(property, dictionary.Values.ElementAt<string>(i));
                property.SetValue(request, propertyValue, null);
            }
        }

 

对可空类型做处理:

 

public static object ConvertToPropType(PropertyInfo property, object value)
        {
            object cstVal = null;
            if (property != null)
            {
                Type propType = Nullable.GetUnderlyingType(property.PropertyType);
                bool isNullable = (propType != null);
                if (!isNullable) { propType = property.PropertyType; }
                bool canAttrib = (value != null || isNullable);
                if (!canAttrib) { throw new Exception("Cant attrib null on non nullable. "); }
                cstVal = (value == null || Convert.IsDBNull(value)) ? null : Convert.ChangeType(value, propType, null);
            }
            return cstVal;
        }

 

对象Transform,对属性赋值,布布扣,bubuko.com

对象Transform,对属性赋值

标签:io   for   ar   cti   new   type   ef   on   

原文地址:http://www.cnblogs.com/Wolfmanlq/p/3906644.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!