简体   繁体   English

C# Cast 仅适用于调试器

[英]C# Cast works only in debugger

I'm developing a mobile application which among other things, it receives data from medical devices via Bluetooth.我正在开发一个移动应用程序,其中包括通过蓝牙从医疗设备接收数据。 To this end, I'm using an Android SDK in JAR via binding to my Xamarin project.为此,我通过绑定到我的 Xamarin 项目在 JAR 中使用 Android SDK。 Here's the class that holds the returned data from the decompiled JAR:这是保存从反编译 JAR 返回的数据的类:

package com.contec.jar.contec08a;
import java.util.ArrayList;

    public class DeviceData
    {
      public ArrayList<byte[]> mData_blood = new ArrayList();
      public ArrayList<byte[]> mData_oxygen = new ArrayList();
      public ArrayList<byte[]> mData_normal_blood = new ArrayList();
    }

What is of interest is the mData_blood.感兴趣的是 mData_blood。 Each element of the array list corresponds to a patient.数组列表的每个元素对应一个患者。 Each byte array is the medical data for each patient.每个字节数组是每个患者的医疗数据。 The Xamarin framework makes some changes, ie the name of the property becomes MDataBlood. Xamarin 框架进行了一些更改,即属性的名称变为 MDataBlood。

Problem : when I receive the above class and property, the casting fails BUT NOT in the debugger.问题:当我收到上述类和属性时,转换失败但不在调试器中。 Here's what I mean: the 'as' operator that implement the cast fails (returns null) but in the debugger the very same expression correctly shows the data.这就是我的意思:实现强制转换的“as”运算符失败(返回 null),但在调试器中,相同的表达式正确地显示了数据。 What's more, the 'is' operator returns false in runtime (the canDo variable is false) but true in the debugger.此外,'is' 运算符在运行时返回 false(canDo 变量为 false),但在调试器中返回 true。 I've tried all methods of casting I'm familiar with, even using –but not mixing – Android objects.我已经尝试了所有我熟悉的转换方法,甚至使用——但不混合——Android 对象。 The highlighted line which casts the IList to List produces a nice exception.将 IList 转换为 List 的突出显示行产生了一个很好的异常。 I'm at a total loss.我完全不知所措。 Any suggestions would be greatly appreciated.任何建议将不胜感激。

Here's the screenshot from the debugger which illustrates all the above:这是调试器的屏幕截图,它说明了上述所有内容:

调试器会话屏幕截图。

Edit1: The MDataBlood[0] evaluates as generic Java object. Edit1:MDataBlood[0] 评估为通用 Java 对象。 When reviewing its properties, its isArray is set to true.查看其属性时,其 isArray 设置为 true。 By checking the decompiled source of the SDK, I determined that is indeed byte array.通过检查SDK的反编译源,我确定它确实是字节数组。

In Xamarin, there are some castings that might fail when the objects come from java types.在 Xamarin 中,当对象来自 java 类型时,有一些转换可能会失败。 In that cases you should use JavaCast<TResult> instead of a regular casting.在这种情况下,您应该使用JavaCast<TResult>而不是常规转换。

Try this, instead of your current approach:试试这个,而不是你目前的方法:

// Since MDataBlood is exposed as an IList property of MDeviceData,
// we first need to cast it to IJavaObject
var dataBloodRaw = (IJavaObject)dm.MDeviceData.MDataBlood;

// Then we have access to its JavaCast method
var dataBlood = dataBloodRaw.JavaCast<JavaList<byte[]>>();

JavaList<T> extends Java.Lang.Object , so it can be used as the TResult parameter for JavaCast<TResult> . JavaList<T>扩展了Java.Lang.Object ,因此它可以用作JavaCast<TResult>TResult参数。 Also, it implements IList , so you should be able to loop through it.此外,它实现了IList ,因此您应该能够遍历它。

Anyway, if you need a List<byte[]> you can use the casted JavaList for creating it:无论如何,如果您需要List<byte[]>您可以使用铸造的JavaList来创建它:

var dataBloodList = dataBlood.ToList();

Or, alternatively或者,或者

var dataBloodList = new List<byte[]>(dataBlood);

Yet another thing you could just try is to Java-cast dataBloodRaw to a JavaList<object> and then loop through its objects, trying to cast each one of them as a byte[] .您可以尝试的另一件事是将 Java dataBloodRawJavaList<object>然后循环遍历其对象,尝试将它们中的每一个转换为byte[]

Plan B B计划

If none of the above works, I suggest you to take a look at this answer where it is suggested to disable linking for Release , which can be done in your project properties:如果上述方法均无效,我建议您查看此答案,建议禁用 Release 链接,这可以在您的项目属性中完成:

在此处输入图片说明

Keep in mind that a side effect of this last option will be the impact on the final size of your application.请记住,最后一个选项的副作用将是对应用程序最终大小的影响。

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

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