gpt4 book ai didi

c# - C#-从类内部调用方法

转载 作者:太空宇宙 更新时间:2023-11-03 17:47:42 25 4
gpt4 key购买 nike

您如何从客户端代码中定义的类内部调用客户端代码方法?

例如,我有一个内存读取类,可以从某个地址的进程的内存中读取值。我还具有用于管理从内存读取的数据类型的类(我正在阅读有关游戏中“对象”的信息。在“客户端代码”中,我正在计算内存中该对象的“基地址”,然后进行初始化)我的“对象类”使用带有“基地址”作为参数的构造函数,然后该基类应该能够通过方法告诉我有关该对象的信息,因为这些对象知道某个值距基地址有多远,例如“健康”)

我尝试使用这样的代码,但它给了我一个错误。 “ ObjectManager”是可以从内存中读取值的类。

class ObjectManager : Memory
{
LocalCharacter LocalPlayer = new LocalCharacter(this);
// other things omitted
}
// Error: Keyword 'this' is not available in the current context


出于绝望:

class ObjectManager : Memory
{
LocalCharacter LocalPlayer = new LocalCharacter(ObjectManager);
// other things omitted
}
// Error: Keyword 'this' is not available in the current context


但无济于事。做这个的最好方式是什么?

最佳答案

因为你没有办法

您必须声明一个方法来访问它。您的主要功能将调用该调用。

如果您希望设置一个类级别的字段,则需要在构造函数中进行设置。您仍然可以在类定义中声明变量(而不是在方法中)

class ObjectManager : Memory
{
public void mymethod()
{
LocalCharacter LocalPlayer = new LocalCharacter(this);
}
}

关于c# - C#-从类内部调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/482800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com