我定义了我的类,我的字段 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;}
}
Run Code Online (Sandbox Code Playgroud)
如何返回“整数”而不是 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"
Run Code Online (Sandbox Code Playgroud)

有一个辅助方法可以获取可空类型的基础值类型:Nullable.GetUnderlyingType。如果给定类型不是封闭的可为空类型,它将返回 null。我会做这样的事情:
Type type = Nullable.GetUnderlyingType(Registro.PropertyType) ?? Registro.PropertyType;
string typeName = type.Name;
Run Code Online (Sandbox Code Playgroud)