C# 指定的强制转换无效-双精度列表到浮点列表

C# 指定的强制转换无效-双精度列表到浮点列表,c#,json,list,casting,C#,Json,List,Casting,所以我在一个JSON文件中存储了一个浮点列表,这是JSON列表的样子: "RollSize": "[17.5,18.0,19.0,23.5,26.5,35.0,35.5,38.0]" 我使用一个方法返回一个对象列表,因为有多个列表要返回。然后,我将对象列表强制转换为浮点。但是,指定的强制转换无效执行此操作时会收到异常。但是,如果我将对象列表转换为一个双精度的值,它就会起作用。以下是两种方法: private void DisplayCutOffs(object sender, EventArg

所以我在一个JSON文件中存储了一个浮点列表,这是JSON列表的样子:

"RollSize": "[17.5,18.0,19.0,23.5,26.5,35.0,35.5,38.0]"
我使用一个方法返回一个对象列表,因为有多个列表要返回。然后,我将对象列表强制转换为浮点。但是,
指定的强制转换无效
执行此操作时会收到异常。但是,如果我将对象列表转换为一个双精度的值,它就会起作用。以下是两种方法:

private void DisplayCutOffs(object sender, EventArgs e) {
            try {
// Errors here unless I cast to double 
                _view.CurrentCutOffValues = _systemVariablesManager.ReturnListBoxValues("CutOff").Cast<float>().ToList();
            }
            catch (Exception ex) {
                LogErrorToView(this, new ErrorEventArgs(ex.Message));
            }
        }
private void DisplayCutOffs(对象发送方,事件参数e){
试一试{
//这里有错误,除非我投双倍
_view.CurrentCutOffValues=_systemVariablesManager.ReturnListBoxValues(“截止”).Cast().ToList();
}
捕获(例外情况除外){
LogErrorToView(这是新的ErrorEventArgs(例如Message));
}
}
存储库方法:

 public List<object> ReturnListBoxValues(string propertyName) {
            if (File.Exists(expectedFilePath)) {
                var currentJsonInFile = JObject.Parse(File.ReadAllText(expectedFilePath));
                return JsonConvert.DeserializeObject<List<object>>(currentJsonInFile["SystemVariables"][propertyName].ToString());
            }
            else {
                throw new Exception("Setup file not located. Please run the Inital Set up application. Please ask Andrew for more information.");
            }
        }
public List ReturnListBoxValues(string propertyName){
if(File.Exists(expectedFilePath)){
var currentJsonInFile=JObject.Parse(File.ReadAllText(expectedFilePath));
返回JsonConvert.DeserializeObject(currentJsonInFile[“SystemVariables”][propertyName].ToString());
}
否则{
抛出新异常(“未找到安装文件。请运行初始安装应用程序。请向Andrew询问更多信息”);
}
}
然而,我注意到,如果我在foreach中循环列表,我可以将每个值转换为float。所以我不确定这里发生了什么


有人知道吗?

听起来你的演员是从
对象
到(类型),其中(类型)要么是
浮动
要么是
双精度
。这是一个取消装箱操作,必须按正确的类型执行。作为
对象的值
,知道它是什么-如果不正确地将其取消装箱,则会引发此异常(警告:如果将其取消装箱到相同大小且兼容的对象,则会有一点回旋余地-例如,可以将int枚举取消装箱到int和v.v)

选项:

  • 坚持使用
    对象
    ,但要知道数据是什么,并正确地取消装箱-可能是在取消装箱后进行铸造,即
    浮点f=(浮点)(双)obj
    (双精度)
    这里是从
    对象
    双精度
    的取消装箱;
    (浮点)
    是从
    双精度
    浮点
    的类型转换)
  • 测试对象类型/使用
    Convert.ToSingle
  • 首先将属性更改为已定义的类型,而不是
    object
完整示例:

List<object> actuallyDoubles = new List<object>{ 1.0, 2.0, 3.0 };
List<double> doubleDirect = actuallyDoubles.ConvertAll(x => (double)x); // works
// List<float> floatDirect = actuallyDoubles.ConvertAll(x => (float)x); // fails per question
List<float> floatViaDouble = actuallyDoubles.ConvertAll(x => (float)(double)x); // works
List<float> floatViaConvert = actuallyDoubles.ConvertAll(x => Convert.ToSingle(x)); // works
List actuallyDoubles=新列表{1.0,2.0,3.0};
List doubleDirect=actuallyDoubles.ConvertAll(x=>(double)x);//作品
//List floatDirect=actuallyDoubles.ConvertAll(x=>(float)x);//每个问题都失败
List floatViaDouble=actuallyDoubles.ConvertAll(x=>(float)(double)x);//作品
List floatViaConvert=actuallyDoubles.ConvertAll(x=>Convert.ToSingle(x));//作品

“[17.5,18.0,19.0,23.5,26.5,35.0,35.5,38.0]”
这是一个字符串而不是数组。你说得对,但它会解析为一个对象列表。double是8个字节,float是4个字节。Linq通常会生成一个可以转换为任何类型的泛型列表对象,但不会将一个类型转换为另一个类型,除非它是显式的。@jdweng那么我的转换是隐式的吗?是的。您正在使用.ToList(),它接受Linq集合并转换为List@Andrew我在下面添加了一些进一步的示例