[英]c# Reflection return “Nullable`1”
我定義了我的班級,我的字段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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.