Ardalan Shahgholi
Ardalan Shahgholi

Reputation: 12555

Type.GetType method returns null value

I have an enum in this namespace:

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

...and am using this method for convert the dataset to a class:

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

However, when I am debugging my code and have one int column in the dataset that converts to an enum property, this line:

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

...returns an error:

Value cannot be null.
Parameter name: conversionType

....and found that:

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

Why is Type.GetType not getting the type of my enum?

Upvotes: 0

Views: 2546

Answers (3)

Ardalan Shahgholi
Ardalan Shahgholi

Reputation: 12555

i fixed this problem with change my code to this :

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

Upvotes: 1

Jeppe Stig Nielsen
Jeppe Stig Nielsen

Reputation: 61912

Just use Nullable.GetUnderlyingType(objProperty.PropertyType) directly.

It's useless to first call ToString() on it, and then use Type.GetType(...) on the string output!

(You make the same mistake in the else block, of course; instead of Type.GetType(objProperty.PropertyType.ToString()), just use objProperty.PropertyType.)

Upvotes: 5

ta.speot.is
ta.speot.is

Reputation: 27214

Disregarding your code, the documentation for Type.GetType states:

Parameters

typeName

The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

...

Return Value

...

The type with the specified name, if found; otherwise, null.

Emphasis mine.

Presumably the type is not in the currently executing assembly, so you'll need to use the assembly-qualified name of the type. Instead of calling ToString(), try using the AssemblyQualifiedName property.

Upvotes: 2

Related Questions