【问题标题】:Type.GetType method returns null valueType.GetType 方法返回空值
【发布时间】:2013-03-17 19:45:51
【问题描述】:

我在这个命名空间中有一个枚举:

Andish.CSS.CommonSilverLight.Enum.Billing.AccountTransacts.AccountTransactAccountType

...并且正在使用此方法将数据集转换为类:

public List<T> ConvertTo<T>(DataTable datatable) where T : new()
    {
        var temp = new List<T>();
        try
        {
            var columnsNames = (from DataColumn dataColumn in datatable.Columns select dataColumn.ColumnName).ToList();
            temp = datatable.AsEnumerable().ToList().ConvertAll<T>(row => GetObject<T>(row, columnsNames));
            return temp;
        }
        catch
        {
            return temp;
        }

    }

 private T GetObject<T>(DataRow row, List<string> columnsName) where T : new()
    {
        T obj = new T();
        try
        {
            string columnname = "";
            string value = "";
            PropertyInfo[] Properties = typeof(T).GetProperties();
            foreach (PropertyInfo objProperty in Properties)
            {
                columnname = columnsName.Find(name => name.ToLower() == objProperty.Name.ToLower());
                if (!string.IsNullOrEmpty(columnname))
                {
                    value = row[columnname].ToString();
                    if (!string.IsNullOrEmpty(value))
                    {
                        if (Nullable.GetUnderlyingType(objProperty.PropertyType) != null)
                        {
                            value = row[columnname].ToString().Replace("$", "").Replace(",", "");
                            objProperty.SetValue(obj, Convert.ChangeType(value,
                                Type.GetType(Nullable.GetUnderlyingType(objProperty.PropertyType).ToString())), null);
                        }
                        else
                        {
                            value = row[columnname].ToString().Replace("%", "");
                            objProperty.SetValue(obj, Convert.ChangeType(value,
                                Type.GetType(objProperty.PropertyType.ToString())), null);
                        }
                    }
                }
            }
            return obj;
        }
        catch
        {
            return obj;
        }
    }

但是,当我调试代码并在数据集中有一个 int 列转换为枚举属性时,这一行:

objProperty.SetValue(obj, Convert.ChangeType(value,
    Type.GetType(Nullable.GetUnderlyingType(objProperty.PropertyType).ToString())), null);

...返回错误:

Value cannot be null.
Parameter name: conversionType

....发现:

Nullable.GetUnderlyingType(objProperty.PropertyType).ToString()
==
"Andish.CSS.CommonSilverLight.Enum.Billing.AccountTransacts.AccountTransactAccountType"
Type.GetType(Nullable.GetUnderlyingType(objProperty.PropertyType).ToString())
==
null

为什么Type.GetType 没有得到我的枚举类型?

【问题讨论】:

    标签: c# reflection


    【解决方案1】:

    直接使用Nullable.GetUnderlyingType(objProperty.PropertyType)即可。

    先在上面调用ToString(),然后在字符串输出上使用Type.GetType(...)是没用的!

    (当然,您在else 块中犯了同样的错误;而不是Type.GetType(objProperty.PropertyType.ToString()),只需使用objProperty.PropertyType。)

    【讨论】:

      【解决方案2】:

      忽略您的代码,the documentation for Type.GetType 声明:

      参数

      类型名称

      要获取的类型的程序集限定名称。见AssemblyQualifiedName。如果类型在当前执行的程序集中或 Mscorlib.dll 中,则提供由其命名空间限定的类型名称就足够了。

      ...

      返回值

      ...

      具有指定名称的类型,如果找到;否则为空。

      强调我的。

      可能该类型不在当前执行的程序集中,因此您需要使用该类型的程序集限定名称。不要调用ToString(),而是尝试使用AssemblyQualifiedName 属性。

      【讨论】:

      • 我像这样更改我的代码 var value = row[columnname]; if (!string.IsNullOrEmpty(value.ToString())) { if (Nullable.GetUnderlyingType(objProperty.PropertyType) != null) { 类型 = Nullable.GetUnderlyingType(objProperty.PropertyType); objProperty.SetValue(obj, Convert.ChangeType(value, type), null); } else { 类型类型 = objProperty.PropertyType; objProperty.SetValue(obj, Convert.ChangeType(value, type), null);但是现在我有这个错误,从“System.Int32”到“AccountTransactAccountType”的无效转换。
      【解决方案3】:

      我解决了这个问题,将我的代码更改为:

      private T GetObject<T>(DataRow row, List<string> columnsName) where T : new()
          {
              T obj = new T();
              try
              {
                  string columnname = "";
      
                  PropertyInfo[] Properties = typeof(T).GetProperties();
                  foreach (PropertyInfo objProperty in Properties)
                  {
                      columnname = columnsName.Find(name => name.ToLower() == objProperty.Name.ToLower());
                      if (!string.IsNullOrEmpty(columnname))
                      {
                          var value = row[columnname];
      
                          if (!string.IsNullOrEmpty(value.ToString()))
                          {
      
                              Type type;
      
                              type = Nullable.GetUnderlyingType(objProperty.PropertyType) ?? objProperty.PropertyType;
      
                              objProperty.SetValue(obj,
                                                   type == value.GetType()
                                                       ? Convert.ChangeType(value, type)
                                                       : System.Enum.ToObject(type, value), null);
                          }
                      }
                  }
                  return obj;
              }
              catch (Exception exception)
              {
                  return obj;
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-09
        • 1970-01-01
        • 2012-02-01
        • 2011-11-18
        • 2011-11-09
        相关资源
        最近更新 更多