Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何返回不同类型的数组?_C#_Python_Arrays_Data Structures_Strong Typing - Fatal编程技术网

C# 如何返回不同类型的数组?

C# 如何返回不同类型的数组?,c#,python,arrays,data-structures,strong-typing,C#,Python,Arrays,Data Structures,Strong Typing,我在C语言中遇到的高级问题是创建一个描述机器人控制网络数据包Ethercat的数据结构的单一副本,然后使用该单一数据结构从数据包集合中提取数据 当试图使用累积数据包中的数据时,会出现问题,因为数据结构与指定类型的强制转换或调用函数存在隐式重复。为了帮助解释这个目标,我编写了一个python程序,它可以实现我想要的功能,并希望能够帮助确定是否可以在C中实现这一点 在C语言中,我面临的挑战是单函数get_vector,它返回一个变量数值类型的齐次集合。此类型在数据包结构中定义,在python中可以使

我在C语言中遇到的高级问题是创建一个描述机器人控制网络数据包Ethercat的数据结构的单一副本,然后使用该单一数据结构从数据包集合中提取数据

当试图使用累积数据包中的数据时,会出现问题,因为数据结构与指定类型的强制转换或调用函数存在隐式重复。为了帮助解释这个目标,我编写了一个python程序,它可以实现我想要的功能,并希望能够帮助确定是否可以在C中实现这一点

在C语言中,我面临的挑战是单函数get_vector,它返回一个变量数值类型的齐次集合。此类型在数据包结构中定义,在python中可以使用,而无需重新定义数据结构

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 vector
    result = [x for x in self.packet_def if x.field_name == name]

    # without error checking, pos contains the location of the definition
    pos = 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 struct
    decoded_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 structure
    def 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 structure
    def 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 pk

    def 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_name
        self.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 program
    packet_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 packets
    pk_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

答案是将数组存储在类型列表的集合中

从集合返回元素的函数的返回类型也应该是动态的


这是我的问题,这一个试图澄清。

我花了很多时间来创建这个问题,并花了几十个小时试图解决这个问题。对我来说,它似乎触及了C语言的基本问题或我对它的理解。如果这显然是一个糟糕的问题,以至于我被投了反对票,请告诉我原因。事实上,我不知道为什么。