zoukankan      html  css  js  c++  java
  • NET中反射实现 可空类型 与基础类型的转换 以及获取指定属性的大小问题

            /// <summary>
            /// 
            /// </summary>
            /// <param name="value">要转换的值</param>
            /// <param name="conversionType">要转换成的类型</param>
            /// <returns></returns>
            private static object ChangeType(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);
            }
    反射 BindingFlags.IgnoreCase 的用法

    反射属性名称,属性名称不区别大小写

    PropertyInfo pi = typeof(object).GetProperty("PropertyName", BindingFlags.IgnoreCase);

    这样是获取不出来的,得加上 BindingFlags.Public | BindingFlags.Instance

    如下这样就可以了。

    PropertyInfo info = obj.GetType().GetProperty(dc.ColumnName, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.GetProperty);
  • 相关阅读:
    《ML in Action》笔记(2) —— ID3决策树
    《ML in Action》笔记(1) —— kNN分类器
    MYSQL笔记
    Javascript代码摘录
    初试mysql存储过程&触发器
    百度地图API应用实践(一) —— 栅格图(草稿)
    2020年8月9日, 网吧, 歌单, 极客时间, 龙岩网络图书馆, 正则, WPS, Python
    2020年8月3日, 网吧 ,
    2020年7月13日,想在网吧搞学习,实属想多了
    账号被盗,什么原因呢?是我的操作系统太脆弱,还是博客园存在安全隐患?
  • 原文地址:https://www.cnblogs.com/guolihao/p/2864978.html
Copyright © 2011-2022 走看看