Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Type.GetType方法返回空值_C#_Reflection - Fatal编程技术网

C# Type.GetType方法返回空值

C# Type.GetType方法返回空值,c#,reflection,C#,Reflection,我在此命名空间中有一个枚举: Andish.CSS.CommonSilverLight.Enum.Billing.AccountTransacts.AccountTransactAccountType …并且我使用此方法将数据集转换为类: public List<T> ConvertTo<T>(DataTable datatable) where T : new() { var temp = new List<T>();

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

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;
        }
    }
…返回一个错误:

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无法获取我的枚举类型?

忽略您的代码,声明:

参数 字体名

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

返回值

具有指定名称的类型(如果找到);否则,无效

我的

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

直接使用Nullable.GetUnderlyingTypeobjProperty.PropertyType即可

首先调用ToString,然后使用Type.GetType是没有用的。。。关于字符串输出


当然,在else块中也会犯同样的错误;不要使用Type.GetTypeobjProperty.PropertyType.ToString,只需使用objProperty.PropertyType。

我通过将代码更改为以下内容修复了此问题:

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;
        }
    }

我更改代码,如下所示:var value=row[columnname];如果string.IsNullOrEmptyvalue.ToString{if Nullable.GetUnderlyingTypeobjProperty.PropertyType!=null{Type Type=Nullable.GetUnderlyingTypeobjProperty.PropertyType;objProperty.SetValueobj,Convert.ChangeTypevalue,Type,null;}其他{Type Type=objProperty.PropertyType;objProperty.SetValueobj,Convert.ChangeTypevalue,Type,null;}}}但是现在我将这个错误从'System.Int32'转换为'accountTransactionType'。
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;
        }
    }