简体   繁体   中英

Object of type 'System.Double' cannot be converted to type 'System.Int32'

I am facing problem in my code, its telling system.int32 cant not be converted to system.double , here is my code

I have an object with 4 string variables and 2 integer variables.

public class MyObject
{
public int id;
public string name1;
public string name2;
public string name3;
public string name4;
public int id2;

}

/*Converts DataTable To List*/
    public static List<TSource> ToList<TSource>(this DataTable dataTable) where TSource : new()
    {
        var dataList = new List<TSource>();

        const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;
        var objFieldNames = (from PropertyInfo aProp in typeof(TSource).GetProperties(flags)
                             select new { Name = aProp.Name, Type = Nullable.GetUnderlyingType(aProp.PropertyType) ?? aProp.PropertyType }).ToList();
        var dataTblFieldNames = (from DataColumn aHeader in dataTable.Columns
                                 select new { Name = aHeader.ColumnName, Type = aHeader.DataType }).ToList();
        var commonFields = objFieldNames.Intersect(dataTblFieldNames).ToList();

        foreach (DataRow dataRow in dataTable.AsEnumerable().ToList())
        {
            var aTSource = new TSource();
            foreach (var aField in objFieldNames)
            {
                PropertyInfo propertyInfos = aTSource.GetType().GetProperty(aField.Name);
                propertyInfos.SetValue(aTSource, dataRow[aField.Name], null);
            }
            dataList.Add(aTSource);
        }
        return dataList;
    }

In my objectfieldname variable i am getting all the variable with right data type, but in dataTblFieldNames variable its converting int to double, so i am facing problem in propertyinfos in foreach loop its saying

Object of type 'System.Double' cannot be converted to type 'System.Int32'.?

Any one help me to fix this please?

You can use Convert.ChangeType method :

Convert.ChangeType(dataRow[aField.Name], propertyInfos.PropertyType)

It will try some numeric conversions (like your conversion from double to int , a change in binary representation from floating point to integer (2's complement)) and some formatting/parsing to/from string .

You have find property type before assigning value and you have to also cast value to the type required because you can not directly assign double type to int type.

if (propertyInfos.PropertyType == typeof(int))
{
    propertyInfos.SetValue(aTSource, Convert.ToInt32(dataRow[aField.Name]), null);
}

Your input dataRow[aField.Name] is of type System.Double while the field you want to assign to is of type System.Int32 . But Visual Studio already told you so loud and clear.

I cannot tell you what to do because I don't know what you want to do. Truncate the double by casting? Round it up or down? Maybe it's the wrong field altogether? we don't know. You decide.

Considering the fact that it's pretty unlikely you came up with the above code and still did not understand the error, maybe it's a good idea to either get a full nights sleep and a good coffee or ask the one who originally wrote this code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM