2016-04-05 48 views
0

我需要將以下代碼從C轉換爲C#。主要問題是結構。我需要能夠通過一個數字來引用這個函數。 任何人都可以幫助我嗎?將C函數指針結構轉換爲C#

void test00(void) 
{ 
    printf("This is test 00\r\n"); 
} 

void test01(void) 
{ 
    printf("This is test 01\r\n"); 
} 

void test02(void) 
{ 
    printf("This is test 02\r\n"); 
} 

typedef struct 
{ 
    int test_number; 
    int (*func)(); 
} test_list_type; 

test_list_type test_list[] = 
{ 
    {0, test00}, 
    {1, test01}, 
    {2, test02} 
}; 

int main() 
{ 
    int i; 

    for (i = 0; i < 3; i++) 
    { 
     test_list[i].func(); 
    } 

    return 0; 
} 
+0

你試過了什麼? –

回答

0

你需要聲明你的結構是這樣的:

struct test_list_type 
{ 
    int test_number; 
    public Action function; 
} 

然後你就可以使結構這樣的一個新實例:

var test = new teststruct 
{ 
    test_number = 0, 
    function = test02 
}; 

並調用類的方法/動作這個:

test.function();