[英]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.GetMemory
和Common.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.GetMemory
和Common.CcApi.GetMemory
也具有byte[]
的返回類型。
編輯:正如喬恩·斯凱特(Jon Skeet)所指出的,似乎Common.TmApi.GetMemory
和Common.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.