繁体   English   中英

无法从“无效”转换为“字节[]”

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

我正在尝试这样做:

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

但是它返回了一个错误:

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方法,该方法看起来像是在调用 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);
}

另一方面,这假定缓冲区恰好是该名称的正确大小。 真的是这样吗? 目前尚不清楚您期望价值来自何处,或者期望价值会持续多久。

现在,您的函数GetMemory没有返回类型(无效)。 更改您的函数public void GetMemory(uint offset, byte[] buffer)以返回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返回byte[]

您的GetMemory方法没有返回类型(它是void )。 因此,您不能在Encoding.ASCII.GetString(PS3.GetMemory(offset, buffer))使用它,因为GetString期望从GetMemory返回一个值。 更改GetMemory方法,使其具有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也具有byte[]的返回类型。

编辑:正如乔恩·斯凯特(Jon Skeet)所指出的,似乎Common.TmApi.GetMemoryCommon.CcApi.GetMemory 不会返回任何值,因此您可能要考虑他的答案或通过以下方式传递“返回”值的类似方法将输出参数传递给GetMemory方法,然后将下一行的值传递给GetString

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

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

GetMemory方法应返回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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM