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

反射动态转换

时间:2014-10-23 19:09:18      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:style   color   io   ar   for   sp   数据   on   cti   

烦,因为反射的时候,有那么多属性数据类型不一样的,然后要写好多转换方法?我不想这样做呢,,所以,找啊找,红色标记为

 

 Type _type = typeof(T);
            System.Reflection.PropertyInfo[] info = _type.GetProperties();
            T t = (T)_type.Assembly.CreateInstance(_type.FullName);
            foreach (var temp in info)
            {
                TextName tn = listTextName.Where(p => p.FieldName == temp.Name).FirstOrDefault();
                if (tn == null) continue;
                var tempValue = tn.TextValue;
                temp.SetValue(t, Convert.ChangeType(tn.TextValue, temp.PropertyType), null);
            }
            return t;

反射动态转换

标签:style   color   io   ar   for   sp   数据   on   cti   

原文地址:http://www.cnblogs.com/Limit-v/p/4046397.html

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