【问题标题】:c# Reflection return "Nullable`1"c#反射返回“Nullable`1”
【发布时间】:2013-12-04 23:07:43
【问题描述】:

我定义了我的类,我的字段 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;}
}

如何返回“整数”而不是 System.DBNull? 我试试这个,但我找不到返回“整数”的方法我刚刚返回 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"

【问题讨论】:

    标签: c# c#-4.0 c#-3.0


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-01-22
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-16
      • 1970-01-01
      • 2011-12-28
      相关资源
      最近更新 更多