C#函数指针?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9754669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:43:15  来源:igfitidea点击:

C# function pointer?

c#pointersmethods

提问by user1276333

I'm having a problem with C#, I'd like to get a pointer of a method in my code, but it seems impossible. I need the pointer of the method because I want to no-op it using WriteProcessMemory. How would I get the pointer?

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

Example code

示例代码

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.
}

回答by Dai

EDIT: I misread your question and didn't see the bit about wanting to NOP a statement with doing raw memory manipulation. I'm afraid this isn't recommended because, as Raymond Chen says, the GC moves stuff around in memory (hence the 'pinned' keyword in C#). You probably can do it with reflection, but your question suggests you don't have a strong grasp of the CLR. Anyway, back to my original irrelevant answer (where I thought you just wanted information on how to use delegates):

编辑:我误读了您的问题,并没有看到想要通过执行原始内存操作来 NOP 声明的一点。恐怕不推荐这样做,因为正如 Raymond Chen 所说,GC 在内存中移动内容(因此是 C# 中的“pinned”关键字)。您可能可以通过反射来做到这一点,但是您的问题表明您对 CLR 没有很好的掌握。无论如何,回到我最初的不相关的答案(我以为你只是想了解如何使用委托的信息):

C# isn't a scripting language ;)

C# 不是脚本语言 ;)

Anyway, C# (and the CLR) has "function pointers" - except they're called "delegates" and are strongly typed, which means you need to define the function's signature in addition to the function you want to call.

无论如何,C#(和 CLR)具有“函数指针”——除了它们被称为“委托”并且是强类型的,这意味着除了要调用的函数之外,您还需要定义函数的签名。

In your case, you'd have something like this:

在你的情况下,你会有这样的事情:

public static void Main(String[] args) {

    Function1();

}

// This is the "type" of the function pointer, known as a "delegate" in .NET.
// An instance of this delegate can point to any function that has the same signature (in this case, any function/method that returns void and accepts a single String argument).
public delegate void FooBarDelegate(String x); 


public static void Function1() {

    // Create a delegate to Function2
    FooBarDelegate functionPointer = new FooBarDelegate( Function2 );

    // call it
    functionPointer("bla");
}

public static void Function2(String x) {

    Console.WriteLine(x);
}

回答by Mahdi

I'd wish it is useful

我希望它有用

class Program
{

    static void Main(string[] args)
    {
        TestPointer test = new TestPointer();
        test.function1();
    }
}
class TestPointer
{
    private delegate void fPointer(); // point to every functions that it has void as return value and with no input parameter
    public void function1()
    {
        fPointer point = new fPointer(function2);
        point();
    }
    private void function2()
    {
        Console.WriteLine("Bla");
    }
}

回答by Mike Zboray

Rewriting a method cannot be done directly from managed code, however the unmanaged .net profiling api can be used to do this. See thismsdn article for example on how to use it.

不能直接从托管代码重写方法,但是可以使用非托管的 .net 分析 API 来做到这一点。有关如何使用它的示例,请参阅msdn 文章。

回答by outbred

I know this is very old, but an example of something like a function pointer in C# would be like this:

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

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

...and then in your main or wherever:

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

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

Then to call the original function,

然后调用原来的函数,

myPointer();
myPointer2();

If you have arguments to your methods, then it's as simple as adding generic arguments to your Action:

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

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

doItWithAString("help me");

Or if you need to return a value:

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

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

var gotIt = getACat("help me");

回答by Thiren Govender

public string myFunction(string name)
{
    return "Hello " + name;
}

public string functionPointerExample(Func<string,string> myFunction)
{
    return myFunction("Theron");
}

Func functionName.. use this to pass methods around. Makes no sense in this context but thats basically how you would use it

Func functionName.. 使用它来传递方法。在这种情况下没有意义,但这基本上就是你将如何使用它