简体   繁体   English

在C#中将Object转换为Ushort数组

[英]Convert Object to an array of Ushort in c#

I'm fairly new to C# but i'm looking to convert an object to an array of unsigned shorts. 我是C#的新手,但我希望将对象转换为无符号短裤数组。 The original data is an array of WORD's (numerical value WORD) passed as an object. 原始数据是作为对象传递的WORD(数字值WORD)的数组。 I've attempted the following but keep getting an error. 我尝试了以下操作,但始终出现错误。

object temp = Agent.Port("PumpPressure1_01").Value;
ushort[] PP1_01 = ((IEnumerable)temp).Cast<object>()
                                     .Select(x => x == null ? x.ToUshort())
                                     .ToArray();

When I run this I get the following error: 运行此命令时,出现以下错误:

'System.Collections.Generic.IEnumerable<T>' requires '1' type arguments.

The namespaces I used when I get the above error are: 出现上述错误时,我使用的命名空间是:

using System.Linq;
using System.Text;   // Don't think this is required but added it in case

If I add the following namespaces: 如果添加以下名称空间:

using System.Collections;
using System.Collections.Generic;

I get the following error. 我收到以下错误。

'System.Linq.ParalleIEnumerable.Select<TTSource,TResult>()' is not supported by the language

I'm sure this is an obvious issue but I've been hunting the net for a while and can't find a solution. 我敢肯定这是一个明显的问题,但是我已经搜寻了一段时间了并且找不到解决方案。 My best guess is that the Select function isn't correct as this was originally designed to convert an object to an array of strings. 我最好的猜测是Select函数是不正确的,因为它最初旨在将对象转换为字符串数组。

Any help would be great. 任何帮助都会很棒。

Thanks 谢谢

IEnumerable is a generic interface, so you have to declare the datatype you are using... IEnumerable是一个通用接口,因此您必须声明所使用的数据类型。

To be honest though, I would want to check what that call to 不过,老实说,我想检查一下该调用

object temp = Agent.Port("PumpPressure1_01").Value;

is actually returning - by inspecting it in the debugger... If it is simply returning a reference to an array of a numeric type, you should be able to simply cast it. 实际上正在返回-通过在调试器中检查它...如果它只是返回对数字类型数组的引用,则应该能够对其进行简单转换。 What you are doing though is trying to cast each individual item within the array - I suspect that's not what you should be doing - which would be casting the array itself. 不过,您正在执行的是尝试将数组中的每个项目都强制转换-我怀疑那不是您应该执行的操作-这将强制转换数组本身。

Can you give us any API documentation for the Port method on the Agent object so I can see what it is meant to return? 您能给我们关于Agent对象上Port方法的任何API文档,以便我了解它返回什么意思吗? Can you try the inspection and see what that gives you? 您可以尝试检查一下,看看能给您带来什么吗?

Why you casting to IEnumerable and then casting it back to object if your temp variable is already of type object ? 为什么你铸造IEnumerable ,然后铸造回object ,如果你的temp变量已经是类型的object

Also IEnumerable<T> is a generic interface and must specify exact type (as exception also says to you). IEnumerable<T>也是一个通用接口,必须指定确切的类型(异常也会告诉您)。 If you have an array of integers and you want to work with them it should be IEnumerable<int> 如果您有整数数组,并且要使用它们,则应为IEnumerable<int>

Thanks for all the help and feedback. 感谢您的所有帮助和反馈。

Unfortunately I was't paying enough attention to the warnings that was posted which seems to be causing the issue. 不幸的是,我对发布的警告似乎未引起足够的重视。

Warning: Reference to type 'System.Func '2' claims it is defined in 'c:\Windows\Microsoft.NET\Framework64\v2.0.50727mscorlib.dll'. but it could not be found

It seems that there is some issue with the .NET reference. .NET参考似乎存在一些问题。 I have another VM which I tested the following solution on and it seemed to work without issue. 我有另一个VM,我在下面的解决方案上进行了测试,它似乎可以正常工作。 Looks like I'll have to reinstall the software package to get it to work on the VM i want to use. 看来我必须重新安装软件包才能在我要使用的VM上运行。

The software package I'm using is a custom package that uses C# to build solutions with prebuilt classes made to look like plug and play blocks. 我正在使用的软件包是一个自定义软件包,该软件包使用C#构建具有预建类的解决方案,这些预构建类看起来像即插即用模块。 You can connect the blocks together drawings lines from one input/output of a block to another. 您可以将块连接在一起,将图线从块的一个输入/输出连接到另一个。 You can then build C# code inside the blocks. 然后,您可以在块内构建C#代码。 Basically c# for dummy's like me.. 基本上是假人的C#,就像我一样。

Example of the blocks: 块示例:

块示例:

As for the code, I did have to make some changes as follows but now works a treat. 至于代码,我确实必须进行如下更改,但现在可以使用了。 Agent.Port("PumpPressure1_01").Value.RawValue is used to reference the particular ports on the block. Agent.Port(“ PumpPressure1_01”)。Value.RawValue用于引用块上的特定端口。

object temp = (object)Agent.Port("PumpPressure1_01").Value.RawValue;

UInt16[] PP1_01 = ((System.Collections.IEnumerable)temp).Cast<object>()
    .Select(x => Convert.ToUInt16(x))
    .ToArray();

foreach(UInt16 x in PP1_01)
{
    Agent.LogDebug("values: " + x.ToString());
}

Again, thanks for all the help. 再次感谢您的所有帮助。 Just need to resolve the issue with the library reference now. 现在只需要使用库参考来解决问题。

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

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