Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c“反射返回”;可为空的'1";_C#_C# 4.0_C# 3.0 - Fatal编程技术网

C# c“反射返回”;可为空的'1";

C# c“反射返回”;可为空的'1";,c#,c#-4.0,c#-3.0,C#,C# 4.0,C# 3.0,我定义了我的类,我的字段pog_02integer和pog_03smallint是可以有空值(int?)的整数 如何返回“integer”而不是System.DBNull? 我尝试了这个,但是我找不到返回“integer”的方法,我刚刚返回了System.DBNull 如果我将字段名称中的“?”作为一个整体删除,则可以识别但不能指定空值。我想帮助我解决这个问题,在这里我可以用“?”定义字段,并且我可以识别整数数据类型,而不是System.DBNull foreach (System.Data.D

我定义了我的类,我的字段pog_02integer和pog_03smallint是可以有空值(int?)的整数

如何返回“integer”而不是System.DBNull? 我尝试了这个,但是我找不到返回“integer”的方法,我刚刚返回了System.DBNull 如果我将字段名称中的“?”作为一个整体删除,则可以识别但不能指定空值。我想帮助我解决这个问题,在这里我可以用“?”定义字段,并且我可以识别整数数据类型,而不是System.DBNull

foreach (System.Data.DataRow dr in dt.Rows){
   object o = Activator.CreateInstance(iSingleType);
   foreach (System.Reflection.PropertyInfo Registro in proRegistro){
     if ((Registro.PropertyType.Namespace != "System.Collections.Generic") && (AtributoLectura(Registro.GetCustomAttributes(true)))){
        TipoDato = Registro.PropertyType.Name;
        if (TipoDato == "Nullable`1") {
            string v = dr[Registro.Name.ToUpper()].GetType().FullName;
            int i = v.IndexOf('.');
            int l = v.Length - i;
            TipoDato = v.Substring(i, l);
        }
        switch (TipoDato){
           case "Char":
           case "String":
              break;
           case "int": case "integer"

有一个助手方法可以获取可空类型的基础值类型:
nullable.getunderyingtype
。如果给定类型不是可为null的封闭类型,它将返回null。我会这样做:

Type type = Nullable.GetUnderlyingType(Registro.PropertyType) ?? Registro.PropertyType;
string typeName = type.Name;

有一个helper方法可以获取可空类型的基础值类型:
nullable.getunderyingtype
。如果给定类型不是可为null的封闭类型,它将返回null。我会这样做:

Type type = Nullable.GetUnderlyingType(Registro.PropertyType) ?? Registro.PropertyType;
string typeName = type.Name;