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