zoukankan      html  css  js  c++  java
  • ASP.NET偷懒大法六(可空类型的动态赋值)

    Nullable是asp.net2.0新增的类型,可以简单的说成是一个泛型结构,对于这种结果前面的章节中动态赋值的方法就不起作用啦。要首先判断他是否是泛型可空型。然后再取出他原始的类型进行赋值

    代码如下:

    System.Reflection.PropertyInfo propertyInfo = t.GetProperty((var as testTextBox).ID);
    if (propertyInfo != null)
    {
                 bool isNullableValueType = propertyInfo.PropertyType.IsGenericType && (propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>));
                Type genericType = propertyInfo.PropertyType;

                 if (isNullableValueType)
                          genericType = propertyInfo.PropertyType.GetGenericArguments()[0];

                           string tempText = (var as testTextBox).Text;
                           if (!string.IsNullOrEmpty(tempText))
                                        propertyInfo.SetValue(entity, Convert.ChangeType(tempText, genericType), null);
    }

    其中

    IsGenericType 判断是否为泛型
    GetGenericTypeDefinition() 取得泛型结构的定义

  • 相关阅读:
    fastlane
    OSI 模型
    iOS面试—0、技术点
    Git 提交规范
    iOS Aggregate 合并静态库
    iOS 应用分发平台
    json 转swift的工具
    敏捷开发
    mac 打包dmg
    iOS 获取素材
  • 原文地址:https://www.cnblogs.com/SoulStore/p/1222911.html
Copyright © 2011-2022 走看看