简体   繁体   中英

c# Concatenate static arrays inside static class

I am not even sure how to call what I want to achieve but... probably the code will explain it.

Basically, I would like to create the frames commands as combinations of some statically defined arrays.

I would like to do something like this:

Command = ConcatArrays(commandPart1, commandPart2, commandPart2)

But it fails inside ConcatArrays as the list elements seem to be null.

And to use externally like this:

Frame.Frame1.Command

The ConcatArrays I took it from here: https://stackoverflow.com/a/3063504/15872

Is it something like this possible?

Thanks a lot for any help, I am quite new to C#.

public static class Frame
{

public class RequestModel
{
    public byte[] Command { get; set; }
    public int ReceiveLength { get; set; }
}

public static RequestModel Frame1 = new RequestModel
{
    Command = ConcatArrays(commandPart1, commandPart2, commandPart2)
    ReceiveLength = 16,
};

public static RequestModel Frame2 = new RequestModel
{
    Command = ConcatArrays(commandPart1, commandPart3)
    ReceiveLength = 16,
};

private static byte[] commandPart1 = new byte[] { 0x1, 0x02 };
private static byte[] commandPart2 = new byte[] { 0x3, 0x4 };
private static byte[] commandPart3 = new byte[] { 0x5, 0x6 };

public static T[] ConcatArrays<T>(params T[][] list)
{
    var result = new T[list.Sum(a => a.Length)];
    int offset = 0;
    for (int x = 0; x < list.Length; x++)
    {
        list[x].CopyTo(result, offset);
        offset += list[x].Length;
    }
    return result;
}

}

You cannot refer to static members below. For example, Frame1 member referencing commandPart1 static member defined below it in the static class.

One way to fix is to define the static members above where referenced. The following tests pass:

    [Test]
    public void Frame1CommandShouldIncludeParts1and2and2()
    {
        var expected = new byte[] {0x1, 0x02, 0x3, 0x4, 0x3, 0x4};
        var actual = Frame.Frame1.Command;
        Assert.AreEqual(expected, actual);
    }

    [Test]
    public void Frame2CommandShouldIncludeParts1and3()
    {
        var expected = new byte[] {0x1, 0x02, 0x5, 0x6};
        var actual = Frame.Frame2.Command;
        Assert.AreEqual(expected, actual);
    }

    public class RequestModel
    {
        public byte[] Command { get; set; }
        public int ReceiveLength { get; set; }
    }

    public static class Frame
    {
        private static readonly byte[] CommandPart1 = { 0x1, 0x02 };
        private static readonly byte[] CommandPart2 = { 0x3, 0x4 };
        private static readonly byte[] CommandPart3 = { 0x5, 0x6 };

        public static RequestModel Frame1 = new RequestModel
        {
            Command = ConcatArrays(CommandPart1, CommandPart2, CommandPart2),
            ReceiveLength = 16
        };
        public static RequestModel Frame2 = new RequestModel
        {
            Command = ConcatArrays(CommandPart1, CommandPart3),
            ReceiveLength = 16
        };

        private static T[] ConcatArrays<T>(params T[][] list)
        {
            var result = new T[list.Sum(a => a.Length)];
            int offset = 0;
            for (int x = 0; x < list.Length; x++)
            {
                list[x].CopyTo(result, offset);
                offset += list[x].Length;
            }
            return result;
        }
    }

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