c#在静态类内连接静态数组 [英] c# Concatenate static arrays inside static class

查看:78
本文介绍了c#在静态类内连接静态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我什至不确定如何调用要实现的目标,但是...代码可能会解释它。

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.

我想做这样的事情:


Command = ConcatArrays(commandPart1,commandPart2,commandPart2)

Command = ConcatArrays(commandPart1, commandPart2, commandPart2)

但是在ConcatArrays内部失败,因为列表元素似乎

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

并像这样在外部使用:


Frame.Frame1 .Command

Frame.Frame1.Command

我从这里获取的ConcatArrays: https://stackoverflow.com/a/3063504/15872

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

有这种可能吗?

非常感谢您的帮助,我对C#还是很陌生。

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

}


推荐答案

您不能在下面引用静态成员。例如, Frame1 成员引用静态类下面定义的 commandPart1 静态成员。

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

这篇关于c#在静态类内连接静态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆