我定义了我的类,我的字段 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"
最佳答案
有一个辅助方法可以获取可空类型的基础值类型:Nullable.GetUnderlyingType
。如果给定类型不是封闭的可空类型,它将返回 null。我会做这样的事情:
Type type = Nullable.GetUnderlyingType(Registro.PropertyType) ?? Registro.PropertyType;
string typeName = type.Name;
关于c# 反射返回 "Nullable` 1",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20388020/