How to return different types of arrays?

2024/10/5 15:20:13

The high level problem I'm having in C# is to make a single copy of a data structure that describes a robot control network packet (Ethercat), and then to use that single data structure to extract data from a collection of packets.

The problem arises when attempting to use the data from the accumulated packets as there is implicit duplication of the data structure with casting or calling functions that specify the type. To assist in explaining the goal, I've written a python program that does what I want and would like help to determine if its possible to do this in C#.

The challenge for me in C# is the single function "get_vector" which returns a homogeneous collection of a variable numerical type. This type is defined in the packet structure and in python can be used without defining re-defining the data structure.

import struct# description of the complete packet
class PACKET_STRUCTURE :# given a field name and a list of packets, return a vector
# this is the function that seems impossible in C# because the type of what is returned changes
def get_vector(self, name, packet_list):# locate the packet definition by the name of the vectorresult = [x for x in self.packet_def if x.field_name == name]# without error checking, pos contains the location of the definitionpos = result[0].position;# decode ALL the pacckets in the (encoded) packet list - returning a list of [time_sec, status, position# in C# this step is similar to  using Marshal.PtrToStructure to transform from byte[] to a structdecoded_packet_list = [struct.unpack(self.fmt_str, packet) for packet in packet_list];# from the list of decoded_packets, extract the desired field into its own list          vector = [decode[pos] for decode in decoded_packet_list]# in C# this is similar to:# var CS_vector = decode_packet_list.Select(item => item.field_name).ToArray();# so far in C# there is no duplication of the packet structure.# but after this point, assume I cast CS_vector to object and return it - # to use the object, I've not figured out how to avoid casting it to some type of array# eg double[], int32[]                        return vector        def __init__(self):self.packet_def = list();self.fmt_str = "<";self.cnt = 0;# add description of single item to the structuredef add(self, struct_def) :struct_def.position = len(self.packet_def);self.packet_def.append(struct_def);self.fmt_str += struct_def.type;                # create a simple packet based on a counter based on the defined structuredef make_packet(self):vals = [self.cnt*10+x for x in range(0, len(self.packet_def))];self.cnt += 1;pk = apply(struct.pack, [self.fmt_str] + vals)# print len(pk), ["%c" % x for x in pk]return pkdef get_names(self):return [packet_items.field_name for packet_items in self.packet_def];# the description of a single field within the packet
class PACKET_ITEM  :def __init__(self, field_name, type):self.field_name = field_nameself.type = type;# self.offset = 0;self.position = 0;if __name__ == "__main__" :INT32 = "l";UINT16 = "H";FLOAT = "f";packet_def = PACKET_STRUCTURE();# create an example packet structure - which is arbituary and could be anything - it could even be read from a file# this definition is the ONLY defintion of the packet structure# changes here require NO changes elsewhere in the programpacket_def.add(PACKET_ITEM("time_sec", FLOAT))packet_def.add(PACKET_ITEM ("status",UINT16))packet_def.add(PACKET_ITEM ("position",INT32))# create a list of packetspk_list = list()for cnt in range(0,10) :pk_list.append(packet_def.make_packet());################################# get the vectors without replicating the structure# eg no int32[] position = (int32[])get_vector()name_list = packet_def.get_names();for name in name_list :vector = packet_def.get_vector(name, pk_list);print name, vector
Answer

The answer is to store the arrays in a collection of type List<dynamic>

The return type of function that returns elements from the collection should also be dynamic.

Here is the more complete answer to my miss-understood question which this one attempted to clarify.

https://en.xdnf.cn/q/119592.html

Related Q&A

How do I pass an array of strings to a python script as an argument?

Ive written a swift app that outputs an array of strings. I would like to import this array into a python script for further processing into an excel file via xlsxwriter, I would like to do this as an …

Match values of different dataframes

This dataframe is the principal with the original tweets. "original_ds_.csv" id tweet --------------------------------------------- 78 "onetoone"…

EOF while parsing

def main():NUMBER_OF_DAYS = 10NUMBER_OF_HOURS = 24data = []for i in range(NUMBER_OF_DAYS):data.append([])for j in range(NUMBER_OF_HOURS):data[i].append([])data[i][j].append(0)data[i][j].append(0)for k …

Why is bool(x) where x is any integer equal to True

I expected bool(1) to equate to True using Python - it does - then I expected other integers to error when converted to bool but that doesnt seem to be the case:>>> x=23 #<-- replace with a…

Getting TypeError while fetching value from table using Python and Django

I am getting error while fetching value from table using Python and Django. The error is below:Exception Type: TypeError Exception Value: not all arguments converted during string formattingMy code…

ValueError: The view **** didnt return an HttpResponse object. It returned None instead

Im using Django forms to handle user input for some point on my Django app. but it keeps showing this error whenever the user tries to submit the form. ValueError: The view *my view name goes here* di…

Game Development in Python, ruby or LUA? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…

Problem with this error: (-215:Assertion failed) !ssize.empty() in function cv::resize OpenCV

I got stuck with this error after running resize function line: import cv2 import numpy as np import matplotlib.pyplot as pltnet = cv2.dnn.readNetFromDarknet(yolov3_custom.cfg, yolov3_custom_last.weigh…

When I run it tells me this : NameError: name lock is not defined?

• Assume that you have an array (data=[]) containing 500,000 elements and that each element has been assigned a random value between 1 and 10 (random.randint(1,10)) .for i in range (500000):data[i]…

Unable to find null bytes in Python code in Pycharm?

During copy/pasting code I often get null bytes in Python code. Python itself reports general error against module and doesnt specify location of null byte. IDE of my choice like PyCharm, doesnt have c…