简体   繁体   English

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

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

I'm trying to do this: 我正在尝试这样做:

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

But it returns me an error: 但是它返回了一个错误:

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

But I don't know why. 但是我不知道为什么。

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

Contrary to the other answer, I don't think you need to modify your GetMemory method, which looks like it's calling void methods (eg here ). 与其他答案相反,我认为您无需修改GetMemory方法,该方法看起来像是在调用 void方法(例如here )。

It looks like GetMemory writes into the buffer you provide, so you may just need: 它看起来像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);
}

On the other hand, that is assuming that the buffer is exactly the right size for the name. 另一方面,这假定缓冲区恰好是该名称的正确大小。 Is that actually the case? 真的是这样吗? It's unclear where you expect the value to come from, or how long you expect it to be. 目前尚不清楚您期望价值来自何处,或者期望价值会持续多久。

Right now your function GetMemory does not have return type(void). 现在,您的函数GetMemory没有返回类型(无效)。 Change your function public void GetMemory(uint offset, byte[] buffer) to return byte[] instead of void . 更改您的函数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);
}

then you can use in this way:- 那么您可以通过以下方式使用:-

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

Assumption:- Common.TmApi.GetMemory and Common.CcApi.GetMemory returning byte[] 假设: Common.TmApi.GetMemoryCommon.CcApi.GetMemory返回byte[]

Your GetMemory method does not have a return type (it's void ). 您的GetMemory方法没有返回类型(它是void )。 Therefore, you can't use it in Encoding.ASCII.GetString(PS3.GetMemory(offset, buffer)) because GetString expects a value to be returned from GetMemory . 因此,您不能在Encoding.ASCII.GetString(PS3.GetMemory(offset, buffer))使用它,因为GetString期望从GetMemory返回一个值。 Change your GetMemory method so it has a return type of 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);
}

As was pointed out in the comments, I'm making the assumption here that Common.TmApi.GetMemory and Common.CcApi.GetMemory also have a return type of byte[] . 正如评论中指出的那样,我在这里假设Common.TmApi.GetMemoryCommon.CcApi.GetMemory也具有byte[]的返回类型。

EDIT: As Jon Skeet has pointed out, it seems that Common.TmApi.GetMemory and Common.CcApi.GetMemory do not return any values, so you may want to consider his answer or a similar approach that passes the "return" value in as an output parameter to your GetMemory method and then passes the value on a subsequent line to GetString . 编辑:正如乔恩·斯凯特(Jon Skeet)所指出的,似乎Common.TmApi.GetMemoryCommon.CcApi.GetMemory 不会返回任何值,因此您可能要考虑他的答案或通过以下方式传递“返回”值的类似方法将输出参数传递给GetMemory方法,然后将下一行的值传递给GetString

As you show in your code, the function GetMemory returns a void (in other words, doesn't return anything). 如您的代码所示,函数GetMemory返回一个void(换句话说,不返回任何内容)。 So you can't pass the return value of that function into another function (in this case the GetString function). 因此,您不能将该函数的返回值传递给另一个函数(在本例中为GetString函数)。

You'll need to find a way to modify GetMemory to return a byte[] array instead, or alternatively find some other way to access that memory that you need. 您将需要找到一种方法来修改GetMemory来返回一个byte[]数组,或者找到其他方法来访问所需的内存。

GetMemory method should return 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