将函数指针作为参数传递给方法 [英] Passing function pointer as a parameter to method

查看:116
本文介绍了将函数指针作为参数传递给方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从C代码编写C#类和方法。我的C代码如下。 function_ex 获取带 custom_pac_type_t 指针的函数指针。问题是关于方法声明。我不能使用Func<>因为有一个ref参数。我必须使用代表,但我不能。如何将func指针传递给 function_ex 方法。谢谢



我尝试过:



c代码:

  typedef   struct  
{
void (* fp)(custom_pac_type_t * pac_variable);
}
custom_hab_type_t;

void function_ex( void (* fp_function)(custom_pac_type_t * pac_variable))
{
custom_hab_type_t-> fp = fp_function;
}

解决方案

参见代表(C#编程指南)| Microsoft Docs [ ^ ]

Hi, i have to write C# class and method from C codes. My C codes are below. function_ex takes function pointer which takes custom_pac_type_t pointer.Problem is about method declaration. i can't use Func<> cause there is a ref parameter. I must use delegates but i couldnt. How can i pass func pointer to function_ex method. Thanks

What I have tried:

c code:

typedef struct
{  
 void (*fp )(custom_pac_type_t* pac_variable);
}
custom_hab_type_t;

void function_ex(void(*fp_function)(custom_pac_type_t* pac_variable))
{  
 custom_hab_type_t->fp = fp_function;
}

解决方案

See Delegates (C# Programming Guide) | Microsoft Docs[^]


这篇关于将函数指针作为参数传递给方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆