无法从 'void' 转换为 'byte[]'

Cannot convert from 'void' to 'byte[]'

我正在尝试这样做:

public string getName(uint offset, byte[] buffer)
{
     return Encoding.ASCII.GetString(PS3.GetMemory(offset, buffer));
}

但它 returns 我出错了:

cannot convert from 'void' to 'byte[]'

但我不知道为什么。

public void GetMemory(uint offset, byte[] buffer)
{
      if (SetAPI.API == SelectAPI.TargetManager)
            Common.TmApi.GetMemory(offset, buffer);
      else if (SetAPI.API == SelectAPI.ControlConsole)
            Common.CcApi.GetMemory(offset, buffer);
}

现在您的函数 GetMemory 没有 return 类型 (void)。将函数 public void GetMemory(uint offset, byte[] buffer) 更改为 return byte[] 而不是 void

public byte[] GetMemory(uint offset, byte[] buffer)
{
      if (SetAPI.API == SelectAPI.TargetManager)
            return Common.TmApi.GetMemory(offset, buffer);
      else if (SetAPI.API == SelectAPI.ControlConsole)
            return Common.CcApi.GetMemory(offset, buffer);
}

那么你可以这样使用:-

public string getName(uint offset, byte[] buffer)
{
     return Encoding.ASCII.GetString(PS3.GetMemory(offset, buffer));
}

假设:- Common.TmApi.GetMemoryCommon.CcApi.GetMemory returning byte[]

您的 GetMemory 方法没有 return 类型(它是 void)。因此,您不能在 Encoding.ASCII.GetString(PS3.GetMemory(offset, buffer)) 中使用它,因为 GetString 期望从 GetMemory 编辑一个值 return。更改您的 GetMemory 方法,使其具有 return 类型的 byte[]:

public byte[] GetMemory(uint offset, byte[] buffer)
{
      if (SetAPI.API == SelectAPI.TargetManager)
            return Common.TmApi.GetMemory(offset, buffer);
      else if (SetAPI.API == SelectAPI.ControlConsole)
             return Common.CcApi.GetMemory(offset, buffer);
}

正如评论中指出的那样,我在这里假设 Common.TmApi.GetMemoryCommon.CcApi.GetMemory 也有一个 return 类型的 byte[]

编辑: 正如 Jon Skeet 指出的那样,似乎 Common.TmApi.GetMemoryCommon.CcApi.GetMemory 没有 return 任何值,因此您可能需要考虑他的答案或类似的方法,将 "return" 值作为输出参数传递给您的 GetMemory 方法,然后将值传递给后续的行到 GetString.

如您在代码中所示,函数 GetMemory return 是一个空值(换句话说,return 没有任何内容)。因此,您不能将该函数的 return 值传递给另一个函数(在本例中为 GetString 函数)。

您需要找到一种方法将 GetMemory 修改为 return 一个 byte[] 数组,或者找到其他方法来访问您需要的内存。

GetMemory 方法应该 return byte[]:

public byte[] GetMemory(uint offset, byte[] buffer)
{
  if (SetAPI.API == SelectAPI.TargetManager)
    return Common.TmApi.GetMemory(offset, buffer);
  else if (SetAPI.API == SelectAPI.ControlConsole)
    return Common.CcApi.GetMemory(offset, buffer);
  else
   throw new NotImplementedException();
}

与其他答案相反,我认为您不需要修改 GetMemory 方法,它看起来像是 调用 void 方法(例如 here).

看起来 GetMemory 写入 您提供的缓冲区,因此您可能只需要:

// Name changed to comply with .NET naming conventions
public string GetName(uint offset, byte[] buffer)
{
    // Populate buffer
    PS3.GetMemory(offset, buffer);
    // Convert it to string - assuming the whole array is filled with useful data
    return Encoding.ASCII.GetString(buffer);
}

另一方面,假设缓冲区 完全 名称的正确大小。事实真的如此吗?不清楚您期望价值从哪里来,或者您期望它有多长。