简体   繁体   中英

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. The original data is an array of WORD's (numerical value WORD) passed as an object. 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.

Any help would be great.

Thanks

IEnumerable is a generic interface, so you have to declare the datatype you are using...

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? 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 ?

Also IEnumerable<T> is a generic interface and must specify exact type (as exception also says to you). If you have an array of integers and you want to work with them it should be 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. I have another VM which I tested the following solution on and it seemed to work without issue. Looks like I'll have to reinstall the software package to get it to work on the VM i want to use.

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. 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. Basically c# for dummy's like me..

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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