【发布时间】:2019-09-07 00:50:36
【问题描述】:
我下载了一个名为Process.NET 的NuGet 包,并尝试在main() 函数中使用IMemory 接口中的Read() 方法。我在GIT 教程中实现了它,但我无法像这样创建ProcessMemory 的实例:
ProcessMemory memory = new ProcessMemory();
我收到此错误:
"Unable to create instance of the abstract class or interface 'ProcessMemory'. "
我找到了一些关于此的线索,但还没有任何帮助。这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Process.NET.Memory;
using Process.NET.Native.Types;
namespace MemoryHacker
{
class Program
{
static void Main(string[] args)
{
ProcessMemory memory = new ProcessMemory();
}
}
//Class with Read() function
public abstract class ProcessMemory : IMemory
{
protected readonly SafeMemoryHandle Handle;
protected ProcessMemory(SafeMemoryHandle handle)
{
Handle = handle;
}
public abstract byte[] Read(IntPtr intPtr, int length);
public string Read(IntPtr intPtr, Encoding encoding, int maxLength)
{
var buffer = Read(intPtr, maxLength);
var ret = encoding.GetString(buffer);
if (ret.IndexOf('\0') != -1)
ret = ret.Remove(ret.IndexOf('\0'));
return ret;
}
public abstract T Read<T>(IntPtr intPtr);
public T[] Read<T>(IntPtr intPtr, int length)
{
var buffer = new T[length];
for (var i = 0; i < buffer.Length; i++)
buffer[i] = Read<T>(intPtr);
return buffer;
}
public abstract int Write(IntPtr intPtr, byte[] bytesToWrite);
public void Write(IntPtr intPtr, string stringToWrite, Encoding encoding)
{
if (stringToWrite[stringToWrite.Length - 1] != '\0')
stringToWrite += '\0';
var bytes = encoding.GetBytes(stringToWrite);
Write(intPtr, bytes);
}
public void Write<T>(IntPtr intPtr, T[] values)
{
foreach (var value in values)
Write(intPtr, value);
}
public abstract void Write<T>(IntPtr intPtr, T value);
}
}
编辑:好的,实例化的事情现在很清楚了。但我仍然收到错误:
"No argument was specified that corresponds to the formal handle parameter of ProcessMemory.ProcessMemory (SafeMemoryHandle)."
看看上面的代码有什么想法吗?
EDIT2:您需要解决这个问题的所有内容都在下面的答案中说明。只是一点提示,如果您使用Visual Studio,然后右键单击新类,然后单击实现。它为你写了很多东西!
【问题讨论】:
-
您需要创建一个派生自
ProcessMemory类的新类。 -
你不能实例化一个抽象类。只有实现可以被实例化。 docs.microsoft.com/en-us/dotnet/csharp/language-reference/…
-
您必须使用派生类之一,例如
LocalProcessMemory。 -
感谢各位的回答,文档帮助很大。我决定重写一个新类中的方法。但是还是报错^^但是这次是:“没有指定与ProcessMemory.ProcessMemory(SafeMemoryHandle)的正式句柄参数相对应的参数。”
标签: c# interface abstract-class implementation