Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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# 为什么params byte[]参数不起作用?_C#_Design Patterns - Fatal编程技术网

C# 为什么params byte[]参数不起作用?

C# 为什么params byte[]参数不起作用?,c#,design-patterns,C#,Design Patterns,我有一个模式扫描仪的这种方法 public static int FindPFM(int Module,long ModuleL,int Offset,params byte[] pattern) { string mask = MaskFromPattern(pattern); int address, val1, val2; address = FindAddress(pattern, 3, mask, Module, Module

我有一个模式扫描仪的这种方法

public static int FindPFM(int Module,long ModuleL,int Offset,params byte[] pattern)
    {

        string mask = MaskFromPattern(pattern);
        int address, val1, val2;

        address = FindAddress(pattern, 3, mask, Module, ModuleL);
        val1 = ReadInt32(scanner.Process.Handle, address);
        address = FindAddress(pattern, 18, mask, Module, ModuleL);
        val2 = ReadByte(scanner.Process.Handle, address);
        val1 = val1 + val2 - Module;
        Offset = val1;
        return Offset;
    }
使用params和b

        localPlayer = FindPFM((0x8D, 0x34, 0x85, 0x00, 0x00, 0x00, 0x00, 0x89, 0x15, 0x00, 0x00, 0x00, 0x00, 0x8B, 0x41, 0x08, 0x8B, 0x48, 0x00), dllClientAddress, dllClientSize, localPlayer);
当我使用它时,它说:“无法将int…转换为byte” 但是0x8D是1字节,这是一个字节向量,为什么会出现这个错误呢

编辑1: 我试过这么做

        localPlayer = FindPFM(new byte[] { 0x8D, 0x34, 0x85, 0x00, 0x00, 0x00, 0x00, 0x89, 0x15, 0x00, 0x00, 0x00, 0x00, 0x8B, 0x41, 0x08, 0x8B, 0x48, 0x00 }, dllClientAddress, dllClientSize, localPlayer);

但是它也不起作用。

当您使用参数数组调用方法时,参数在末尾,而不是开头(以匹配参数数组位置)。接下来,参数不会像您尝试的那样放在括号中-这是C#7元组文本的语法。从参数名和参数名来区分有点棘手,但我认为您需要:

localPlayer = FindPFM(dllClientAddress, dllClientSize, localPlayer,
     0x8D, 0x34, 0x85, 0x00, 0x00, 0x00, 0x00, 0x89, 0x15, 0x00,
     0x00, 0x00, 0x00, 0x8B, 0x41, 0x08, 0x8B, 0x48, 0x00);
下面是一个完整的示例:

using System;

public class Test
{
    static void Main()
    {
        Foo(10, 20, 0x80, 0x8d, 0xff);
    }

    static void Foo(int x, int y, params byte[] bytes)
    {
        Console.WriteLine($"x: {x}");
        Console.WriteLine($"y: {y}");
        Console.WriteLine($"bytes: {BitConverter.ToString(bytes)}");
    }
}

您希望这些参数中的哪一个对应于
模式
参数,为什么?括号中以逗号分隔的值列表将用于C#7元组文字,但不用于参数数组。在方法delcaration中,第一个参数是int,最后一个是byte[]。在调用代码示例中,有一个“字节”元组。