简体   繁体   English

类型'System.Int16'的对象不能转换为类型'System.Nullable`1 [System.Int32]

[英]Object of type 'System.Int16' cannot be converted to type 'System.Nullable`1[System.Int32]

I've a method that generates list of class type from data of data reader. 我有一种从数据读取器的数据生成类类型列表的方法。

   if (datareader != null && datareader .HasRows)
   {

                Dictionary<string, PropertyInfo> pDict= GetPropertyDictionary<T>();
                var fields = GetFieldNames(datareader );
                while (datareader .Read())
                {
                    T myobj= new T();
                    for (int index = 0; index < fields.Count; index++)
                    {                        
                        if (pDict.TryGetValue(fields[index], out PropertyInfo info))
                        {

                                var val1 = datareader .GetValue(index);                                
                                info.SetValue(myobj, (val1 == DBNull.Value) ? null : val1, null);

                        }
                    }

                }

            }

I have class properties, some of them are nullable. 我有类属性,其中一些是可为空的。

public string StudentName{ get; set; }
public decimal? percentage{ get; set; }
public int? StudentNumber{ get; set; }

Code works properly for all properties except StudentNumber which is int. 代码对所有属性均适用,但StudentNumber为int除外。

In above code following line throws exeption Object of type 'System.Int16' cannot be converted to type 'System.Nullable`1[System.Int32] : 在上面的代码中,以下行引发'System.Int16'类型的对象对象不能转换为'System.Nullable`1 [System.Int32]类型

info.SetValue(myobj, (val1 == DBNull.Value) ? null : val1, null); info.SetValue(myobj,(val1 == DBNull.Value)?null:val1,null);

What can be done to solve this issue? 如何解决这个问题?

I don't agree with this code for many reasons but to solve your current problem and answer your question it's because you can't explicitly convert Int16 to Int32 nor to Nullable<Int32> or int? 我出于很多原因不同意此代码,但是为了解决您当前的问题并回答您的问题,这是因为您无法将Int16显式转换为Int32Nullable<Int32>int? .

To make this happen you need to first convert the value to Int32 then to Nullable<Int3> . 为此,您需要先将值转换为Int32然后转换为Nullable<Int3>

There are better ways but to make it clear what's going on and to fix this one error here you go... 有更好的方法可以让您弄清楚发生了什么并在此处修复此错误...

info.SetValue(myobj, val1 == DBNull.Value ? null : (int?)Convert.ToInt32(val1), null);

The problem that you're having here is that, while you can cast a short to an int , you can't cast a boxed short to an int directly. 您在这里遇到的问题是,尽管您可以将short转换为int ,但是不能直接将boxed short强制转换为int ie,

object box = (short)5;
var x = (int?)box; // Invalid cast.
var y = (int?)(short?)box; // fine, we cast to short and then to int.

You could change the property type of your class to short? 您可以将类的属性类型更改为short? , or you could check if your property's type is Nullable<int32> and use Convert.ToInt32 on the value under that situation. ,或者您可以检查属性的类型是否为Nullable<int32>然后在这种情况下对值使用Convert.ToInt32

Try to change type: 尝试更改类型:

var val1 = datareader.GetValue(index);    
var convertedValue = (val1 == DBNull.Value) ? null : Convert.ChangeType(val1, info.PropertyType);                            
info.SetValue(myobj, convertedValue, null);

暂无
暂无

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

相关问题 类型&#39;System.Nullable`1 [System.Int32]&#39;的表达式不能用于类型&#39;System.Int32&#39;\\ r \\ n的构造函数参数名称:arguments [0] - Expression of type 'System.Nullable`1[System.Int32]' cannot be used for constructor parameter of type 'System.Int32'\r\nParameter name: arguments[0] Expression.Convert:'System.Int64'类型的对象无法转换为'System.Int32'类型 - Expression.Convert: Object of type 'System.Int64' cannot be converted to type 'System.Int32' “System.Int32”类型的对象无法转换为“System.UInt32&”类型 - Object of type 'System.Int32' cannot be converted to type 'System.UInt32&' C#无法将类型为&#39;system.int16 [*]&#39;的对象转换为类型为&#39;system.Int16 []&#39;的对象 - c# unable to cast object of type 'system.int16[*]' to type 'system.Int16[]' 无法将类型为“ 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' JSON 值无法转换为 System.Nullable[System.Int32] - The JSON value could not be converted to System.Nullable[System.Int32] 从“System.Int32”到“System.Nullable”1[[System.Int32, mscorlib]] 的无效转换 - Invalid cast from 'System.Int32' to 'System.Nullable`1[[System.Int32, mscorlib]] 无法将类型为“ System.Double”的对象转换为类型为“ System.Int32”的对象 - Object of type 'System.Double' 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”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM