c# - 如何在 C# 中使用具有多个参数的委托函数
问题描述
我有委托功能,即:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void AddMultiInformation([MarshalAs(UnmanagedType.LPStr)] string str,
[MarshalAs(UnmanagedType.LPStr)] string str2,
[MarshalAs(UnmanagedType.LPStr)] string str3);
我在 C DllImported 函数的回调中使用此委托函数,如下所示。
[DllImport("CarsDLL.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
internal static extern int GetCarsInformations(AddMultiInformation carsData);
请问我可以使用哪种类型的参数来正确调用 GetCarsInformations 函数?
class Program
{
// Constructor
protected Program()
{
}
// Main
[STAThread]
public static void Main()
{
//GetCarsInformations();
Application.Exit();
}
}
解决方案
匹配您代表的签名
public delegate void AddMultiInformation([MarshalAs(UnmanagedType.LPStr)] string str,
[MarshalAs(UnmanagedType.LPStr)] string str2,
[MarshalAs(UnmanagedType.LPStr)] string str3);
你需要一个方法
- 退货
void
- 恰好有三个参数,它们都是
string
.
所以,像
public void MethodThatAddsMultiInformation(string str, string str2, string str3)
{
// put some logic in here
}
然后,您可以通过为其AddMultiInformation
分配对方法的引用来创建委托的实例MethodThatAddsMultiInformation
:
AddMultiInformation addMultiInformation = MethodThatAddsMultiInformation;
然后GetCarsInformations
用委托的实例调用:
var information = GetCarsInformations(addMultiInformation);
推荐阅读
- javascript - 从平面列表中删除项目
- google-chrome-extension - Chrome 扩展清单 V3 远程托管代码
- java - 春天的依赖不满足
- performance - Apache Ignite CollisionSpi 配置
- c# - 如何初始化 vhdx 并格式化以将内容复制到新驱动器
- uwp - 带圆角的内容对话框 [UWP Win10 Build 1904*]
- algorithm - 系统搜索算法时间复杂度
- mysql - 从原始线索子源不为空的贷款中选择前七个字符或原始线索子源,限制为 10
- reactjs - 如何在 react.js 中创建动态网格?
- html - 为什么 node.js 服务器会两次打印 html 表单的值?