c# Concatenate static arrays inside static class

笑着哭i 提交于 2019-12-11 15:10:51

问题


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;
}

}

回答1:


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;
        }
    }


来源:https://stackoverflow.com/questions/49060954/c-sharp-concatenate-static-arrays-inside-static-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!