简体   繁体   English

无法将类型为“ System.Double”的对象转换为类型为“ System.Int32”的对象

[英]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 我在我的代码中system.int32问题,它不能告诉system.int32转换为system.double ,这是我的代码

I have an object with 4 string variables and 2 integer variables. 我有一个带有4个字符串变量和2个整数变量的对象。

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 在我的objectfieldname变量中,我正在获取具有正确数据类型的所有变量,但是在dataTblFieldNames变量中,其将int转换为double,因此我在foreach loopdataTblFieldNames propertyinfos中的问题

Object of type 'System.Double' cannot be converted to type 'System.Int32'.? 类型“ System.Double”的对象不能转换为类型“ System.Int32”。

Any one help me to fix this please? 有人帮我解决这个问题吗?

You can use Convert.ChangeType method : 您可以使用Convert.ChangeType方法

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 . 它将尝试一些数字转换(例如,从doubleint的转换,将二进制表示形式从浮点更改为整数(2的补码)),以及一些对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. 在分配值之前,您必须先找到属性类型,并且还必须将值强制转换为所需的类型,因为您不能直接将double类型分配给int类型。

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 . 输入dataRow[aField.Name]的类型为System.Double而您要分配的字段的类型为System.Int32 But Visual Studio already told you so loud and clear. 但是Visual Studio已经告诉您如此清晰明了。

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. 考虑到您不太可能想到上面的代码并且仍然不理解错误,因此,最好是整夜睡眠和喝杯咖啡,或者询问最初编写此代码的人,这是一个好主意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.InvalidCastException:无法将“System.Double”类型的 object 转换为代码中的“System.Int32”类型 - System.InvalidCastException: Unable to cast object of type 'System.Double' to type 'System.Int32' in code 从具体化的“System.Double”类型到“System.Int32”类型的指定转换无效但没有定义 Double - The specified cast from a materialized 'System.Double' type to the 'System.Int32' type is not valid But no Double defined 类型&#39;System.Int16&#39;的对象不能转换为类型&#39;System.Nullable`1 [System.Int32] - Object of type 'System.Int16' cannot be converted to type 'System.Nullable`1[System.Int32] 从具体化的“System.Int32”类型到“System.Double”类型的指定转换无效 - The specified cast from a materialized 'System.Int32' type to the 'System.Double' type is not valid “System.Int32”类型的对象无法转换为“System.UInt32&”类型 - Object of type 'System.Int32' cannot be converted to type 'System.UInt32&' Expression.Convert:'System.Int64'类型的对象无法转换为'System.Int32'类型 - Expression.Convert: Object of type 'System.Int64' cannot be converted to type 'System.Int32' ASP.NET FormView:“类型&#39;System.Int32&#39;的对象不能转换为类型&#39;System.String” - ASP.NET FormView: “Object of type 'System.Int32' cannot be converted to type 'System.String” 无法将类型为“ System.Reflection.ParameterInfo”的对象转换为类型为“ System.Int32”的对象 - Object of type 'System.Reflection.ParameterInfo' cannot be converted to type 'System.Int32' “System.Int32”类型的对象无法转换为“System.Web.Security.Cryptography.Purpose”类型 - Object of type 'System.Int32' cannot be converted to type 'System.Web.Security.Cryptography.Purpose' MSTest - DataTestMethod 抛出异常 System.ArgumentException:“System.Decimal”类型的对象无法转换为“System.Double”类型 - MSTest - DataTestMethod throws exception System.ArgumentException: Object of type 'System.Decimal' cannot be converted to type 'System.Double'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM