2012-03-17 16 views
17

C#で問題が発生しました。私のコードでメソッドのポインタを取得したいのですが、不可能と思われます。 WriteProcessMemoryを使用していないので、メソッドのポインタが必要です。どのようにポインタを取得できますか?C#関数ポインタ?

例コード

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

これは有効なC#コードではありません。あなたは何をしようとしているのですか? – gdoron

+4

はこの質問に似ているようです(こちら)[こちら](http://stackoverflow.com/questions/2550218/how-to-store-a-function-pointer-in-c-sharp)。それはあなたを助けるかもしれません。 –

+1

あなたは(完全に)間違った方法で問題に近づいています。なぜあなたはこのメソッドを使用したくないのですか?あなたはそれを行うことができます - しかし、あなたの呼び出しコードに応じて異なっています。 –

答えて

19

編集:私はあなたの質問を読み違えると、生のメモリ操作を行うとの声明をNOPしたいについてのビットを見ていません。レイモンド・チェンが言っているように、GCがメモリ内で動き回るので(これはC#の「固定された」キーワードなので)、これはお勧めできません。あなたはおそらく反射でそれを行うことができますが、あなたの質問は、あなたがCLRを強く把握していないことを示唆しています。とにかく、戻って私の元無関係の答え(私はあなただけのデリゲートを使用する方法に関する情報を望んでいたと思ったところ)へ:

C#はスクリプト言語ではありません。)とにかく

、C#の(およびCLR)は、「持っています関数ポインタ " - それは"デリゲート "と呼ばれ、強く型付けされていることを除きます。つまり、呼び出す関数に加えて関数のシグネチャを定義する必要があります。しかし、

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

あなたはそれを_can_ nop(並べ替え)します。本体を持たない 'Function3'を作成し、デリゲートを' Function3'に変更し、 'Main'をデリゲートを呼び出します。 –

2

が、私はメソッドを書き換え

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"); 
    } 
} 
1

は、マネージコードから直接実行することはできません役立ちたいと思います:あなたのケースでは

、あなたはこのような何かを持っていると思います管理していない.netプロファイリングAPIを使用してこれを行うことができます。たとえば、使用方法については、this msdnの記事を参照してください。

18

私は、これは非常に古いですけど、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<string> doItWithAString = null; 
doItWithAString = temp.DoSomethingWithAString; 

doItWithAString("help me"); 

をそれとも、値を返す必要がある場合:あなたの行動に、一般的な引数を追加

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

var gotIt = getACat("help me"); 
+1

うわー、素晴らしい答え。多分この質問に固有ではないかもしれませんが、私は何かを学び、私の問題を解決しました。 – pelesl

+0

+1ベストアンサー!各シナリオの例を共有してくれてありがとう(void - > void、args - > void、args - > return) – bigp

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

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

のFunc functionNameをし...周りのメソッドを渡すために、これを使用しています。この文脈では意味がありませんが、基本的にどのように使用するのでしょうか

+1

これは新しい受け入れられた答えでなければなりません。 – Roman