2014-08-28 93 views

回答

2

簡短回答:是的。

一般:
(未經測試...只是一個大綱)

{ 
    bool AFunction(ref int x, params object[] list) 
    { 
     /* Some Body */ 
    } 

    public delegate bool Proc(ref int x, params object[] list); // Declare the type of the "function pointer" (in C terms) 

    public Proc my_proc; // Actually make a reference to a function. 

    my_proc = AFunction;   // Assign my_proc to reference your function. 
    my_proc(ref index, a, b, c); // Actually call it. 
} 
+1

'proc'是一個*類型*。 'proc =函數;'不會編譯。 (我可以看到這個*「未經測試...只是一個大綱」*,但測試它。) – 2014-08-28 20:41:54

+1

啊 - params關鍵字是我沒有意識到的部分。非常感謝。 – KairisCharm 2014-08-28 21:30:03

0

請閱讀delegates。 不完全等同,但用於類似目的。

樣品:

public delegate void DoSth(string message); 

void foo(string msg) { 
    // ... 
} 


void bar() { 
    DoSth delg = foo; 
    delg("tttttt"); 
} 

如果你正在嘗試從調用,使用P本地C++庫中的代碼/ Invoke的,你需要看看GetDelegateForFunctionPointerGetFunctionPointerForDelegate Method對功能。

+0

問題downvoter - 爲什麼? – Krizz 2014-08-28 20:25:38

+2

但我不是downvoter,以*「Read up on ...」開頭的答案通常不是真正的答案。 – abelenky 2014-08-28 20:26:24

+0

你也可能打算做'DoSth delg = foo;'。 – Mephy 2014-08-28 20:58:19