Type.GetType方法返回null值

Ard*_*oli 0 c# reflection

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

Andish.CSS.CommonSilverLight.Enum.Billing.AccountTransacts.AccountTransactAccountType
Run Code Online (Sandbox Code Playgroud)

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

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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

objProperty.SetValue(obj, Convert.ChangeType(value,
    Type.GetType(Nullable.GetUnderlyingType(objProperty.PropertyType).ToString())), null);
Run Code Online (Sandbox Code Playgroud)

...返回错误:

Value cannot be null.
Parameter name: conversionType
Run Code Online (Sandbox Code Playgroud)

....并发现:

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

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

Jep*_*sen 5

Nullable.GetUnderlyingType(objProperty.PropertyType)直接使用.

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

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