C#函数指针?

标签 c# pointers methods

我在使用 C# 时遇到问题,我想在我的代码中获取一个方法的指针,但这似乎是不可能的。我需要方法的指针,因为我想使用 WriteProcessMemory 不对其进行操作。我将如何获得指针?

示例代码

main()
{
    function1();
    function2();
}

function1()
{
    //get function2 pointer
    //use WPM to nop it (I know how, this is not the problem)
}
function2()
{
    Writeline("bla"); //this will never happen because I added a no-op.
}

最佳答案

我知道这已经很老了,但是 C# 中的函数指针之类的示例如下所示:

class Temp 
{
   public void DoSomething() {}
   public void DoSomethingElse() {}
   public void DoSomethingWithAString(string myString) {}
   public bool GetANewCat(string name) { return true; }
}

...然后在您的主要或任何地方:

var temp = new Temp();
Action myPointer = null, myPointer2 = null;
myPointer = temp.DoSomething;
myPointer2 = temp.DoSomethingElse;

然后调用原来的函数,

myPointer();
myPointer2();

如果您的方法有参数,那么它就像向您的 Action 添加通用参数一样简单:

Action<string> doItWithAString = null;
doItWithAString = temp.DoSomethingWithAString;

doItWithAString("help me");

或者如果你需要返回一个值:

Func<string, bool> getACat = null;
getACat = temp.GetANewCat;

var gotIt = getACat("help me");

关于C#函数指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9754669/

相关文章:

c - 将字符数组的指针发送到C中的函数

jquery - 使用 jQuery 从元素中获取特定文本内容

c# - MVC : File. 存在返回 false

c# - 在 ASP.NET 中使用线程是否有任何不明显的危险?

c# - 如何在不安装数据库的情况下使用数据库?

c - 指向整数的指针数组?或指向整数数组的指针?

c# - 高质量图像缩放

C:将数组(指针)传递给函数

java - 创建用于设置 GridBagConstraints 的方法

java - final方法是内联的?