简体   繁体   English

C#反射返回“ Nullable`1”

[英]c# Reflection return “Nullable`1”

I define my class, my fields pog_02integer and pog_03smallint are integer that can have null value (int?) 我定义了我的班级,我的字段pog_02integer和pog_03smallint是可以为空值(int?)的整数。

public class v_cCfgDeclaraciones {
  public string pog_idcotizacion {get;set;}
  public string pog_01varchar  {get;set;}
  public int? pog_02integer  {get;set;}
  public int? pog_03smallint  {get;set;}
  public DateTime? pog_04date  {get;set;}
}

How I can return "integer" and not System.DBNull? 如何返回“整数”而不是System.DBNull? I try this, but I can't find a way to return "integer" I just returned System.DBNull if I remove "?" 我尝试了这个,但是我找不到返回“整数”的方法,如果我删除了“?”,我只是返回了System.DBNull。 in the name of the field as a whole recognizes but can't assign a null value. 整个字段名称中的,可以识别但不能分配空值。 I would like to help me resolve this problem, where I can define the field with "?" 我想帮助我解决此问题,我可以在其中定义“?”字段 and I recognize the integer data type and not 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"

图片

There is a helper method to get a nullable type's underlying value type: Nullable.GetUnderlyingType . 有一个帮助程序方法可获取可空类型的基础值类型: Nullable.GetUnderlyingType It will return null if the given type is not a closed over nullable type. 如果给定类型不是可空类型的封闭类型,它将返回null。 I would do something like this: 我会做这样的事情:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM