C# - 从类内部调用方法

时间:2022-03-10 15:53:19

How do you call a client codes methods from inside a class defined in the client code?

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

For example, I have a memory reading class, that can read values from the memory of a process at a certain address. I also have classes for managing the type of data that is read from memory (I am reading about an in-game 'object'. In the 'client code' I am calculating the 'base address' of that object in memory, then initializing my 'object class' using a constructor that takes the 'base address' as a parameter. This base class should then be able to tell me things about that object through methods because the objects know how far away from the base address a certain value is, such as 'health')

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

I tried using code such as this, and it gave me an error. 'ObjectManager' is the class which can read values from memory.

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

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

and this, out of desperation:

出于绝望:

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

But to no avail. What is the best way to do this?

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

2 个解决方案

#1


because you are not in a method.

因为你不是一个方法。

You must declare a method to access this. Your main function would invoke the call.

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

If you wish to set a class level field then you will need to do it in the constructor. You still declare the variable in your class definition though (not in a method)

如果您希望设置类级别字段,则需要在构造函数中执行此操作。您仍然在类定义中声明变量(不在方法中)

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

#2


How about referencing 'this' in the constructor:-

如何在构造函数中引用'this': -

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

    LocalCharacter LocalPlayer;
    // other things omitted
}

#1


because you are not in a method.

因为你不是一个方法。

You must declare a method to access this. Your main function would invoke the call.

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

If you wish to set a class level field then you will need to do it in the constructor. You still declare the variable in your class definition though (not in a method)

如果您希望设置类级别字段,则需要在构造函数中执行此操作。您仍然在类定义中声明变量(不在方法中)

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

#2


How about referencing 'this' in the constructor:-

如何在构造函数中引用'this': -

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

    LocalCharacter LocalPlayer;
    // other things omitted
}