C# function pointer?

前端 未结 5 1952
情话喂你
情话喂你 2020-12-25 12:40

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

相关标签:
5条回答
  • 2020-12-25 12:45

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

    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<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");
    
    0 讨论(0)
  • 2020-12-25 12:47
    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

    0 讨论(0)
  • 2020-12-25 12:50

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

    0 讨论(0)
  • 2020-12-25 12:51

    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):

    C# isn't a scripting language ;)

    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.

    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);
    }
    
    0 讨论(0)
  • 2020-12-25 13:10

    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");
        }
    }
    
    0 讨论(0)
提交回复
热议问题