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() 取得泛型结构的定义

  • 相关阅读:
    python 数据分析3
    python 数据分析2
    Python 数据分析1
    Python18 Django 基础
    Python 17 web框架&Django
    一只救助犬的最后遗言
    With As 获取 id parentId 递归获取所有
    分布式事物
    div 浮动框
    sql时间比较
  • 原文地址:https://www.cnblogs.com/SoulStore/p/1222911.html
Copyright © 2011-2022 走看看