如何在静态类中使用具有接口作为参数的C#回调函数?
为了支持特定的硬件,我必须在C库(sld)的开源C#wrapper(tao.sdl)中调整几个函数。在这个包装器中,所有C函数都封装在一个静态类中 不幸的是,C#lib中的两个回调函数允许访问前面提到的特定硬件,它们需要一个实现两个接口的类作为参数。因此,这一个必须是非静态的…并且,据我所知,在第一个库中不能引用相应的实例 如何从设备捕获事件(通过可用的回调)并在修改后的C#包装器类中使用它们?这一个必须保持静态,因为修改对于客户端的应用程序必须是透明的 以下是接口和回调定义:如何在静态类中使用具有接口作为参数的C#回调函数?,c#,class,interface,static,callback,C#,Class,Interface,Static,Callback,为了支持特定的硬件,我必须在C库(sld)的开源C#wrapper(tao.sdl)中调整几个函数。在这个包装器中,所有C函数都封装在一个静态类中 不幸的是,C#lib中的两个回调函数允许访问前面提到的特定硬件,它们需要一个实现两个接口的类作为参数。因此,这一个必须是非静态的…并且,据我所知,在第一个库中不能引用相应的实例 如何从设备捕获事件(通过可用的回调)并在修改后的C#包装器类中使用它们?这一个必须保持静态,因为修改对于客户端的应用程序必须是透明的 以下是接口和回调定义: public i
public interface IDataHandler
{
void HandleHidData(bytes[]);
}
public void SetDataCallback(IDataHandler handler)
请考虑我是C语言中的新手,所以我的问题可能有一个简单的答案,但是我自己或网络上找不到它……
非常感谢你的帮助
附言:根据要求,你可以在下面找到(尽可能简短的)问题摘要
namespace Sample
{
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
//////////////////////////////////////////////////////
// Methods definitions giving access to the new device
// Sources of this DLL are NOT provided.
//////////////////////////////////////////////////////
public partial class NewDeviceLib
{
// ...
public interface IFirstInterface
{
void HandleData(byte[] data);
}
public static NewDeviceLib GetDevice ();
public void SetDataCallback(IFirstInterface handler);
// ...
}
////////////////////////////////////////////
// Custom class implementing IFirstInterface
////////////////////////////////////////////
public class NewDeviceHandler: NewDeviceLib.IFirstInterface
{
// Storage of collected data to be processed
List<byte[]> m_data = new List<byte[]>();
// This callback is triggered each time device state changes
public void HandleData (byte[] data)
{
// Stores data for future use in C# wrapper
m_data.Add(data);
}
public List<byte[]> GetPendingData ()
{
return m_data;
}
// Clear processed data
public void ClearPendingData()
{
m_data.RemoveAt(0);
}
}
////////////////////////////////////////////////////////////////////
// Open source lib to be modified in order to support the new device
////////////////////////////////////////////////////////////////////
public struct Event
{
// public structures defining the events
public int type;
public byte[] data;
// ...
}
// HYPOTHETICAL NON static implementation of open source lib
// to be adapted to support new device type
public class OpenSourceLibWrapperClass
{
// ....
// adds device ref to support new device
NewDeviceLib m_device = null;
// adds instance of NewDeviceLib (this must be non static)
NewDeviceHandler m_newDeviceHandler;
// Initialization of all connected devices
private void InitNewDevice ()
{
// added to support new device type
m_device = NewDeviceLib.GetDevice();
if (m_device != null)
{
m_device.SetDataCallback(m_newDeviceHandler);
}
}
// Existing method wrapping C function library
[DllImport("DLL_NAME", EntryPoint = "_Init")]
private static extern void _Init();
public void Init ()
{
// added to initialize new device
InitNewDevice();
// calls external C function for other devices
_Init();
}
// Existing method wrapping C function library
// returns 0 when all pending events have been processed
[DllImport("DLL_NAME", EntryPoint = "_PollEvent")]
private static extern int _PollEvent(out Event DevEvent);
public int PollEvent(out Event DevEvent)
{
// Added to support new device
if (m_device != null)
{
if (m_newDeviceHandler.GetPendingData().Count > 0)
{
DevEvent = new Event();
DevEvent.type = 9999;
DevEvent.data = m_newDeviceHandler.GetPendingData()[0];
m_newDeviceHandler.ClearPendingData();
return m_newDeviceHandler.GetPendingData().Count;
}
}
// usual process for events of other devices
return _PollEvent(out DevEvent);
}
}
}
名称空间示例
{
使用System.Collections.Generic;
运用系统反思;
使用System.Runtime.InteropServices;
//////////////////////////////////////////////////////
//允许访问新设备的方法定义
//未提供此DLL的源。
//////////////////////////////////////////////////////
公共部分类NewDeviceLib
{
// ...
公共接口IFirstInterface
{
void HandleData(字节[]数据);
}
公共静态NewDeviceLib GetDevice();
public void SetDataCallback(IFirstInterface处理程序);
// ...
}
////////////////////////////////////////////
//实现IFirstInterface的自定义类
////////////////////////////////////////////
公共类NewDeviceHandler:NewDeviceLib.IFirstInterface
{
//待处理的收集数据的存储
列表m_数据=新列表();
//每次设备状态更改时都会触发此回调
公共无效句柄数据(字节[]数据)
{
//将数据存储在C#包装器中以备将来使用
m_data.Add(数据);
}
公共列表GetPendingData()
{
返回m_数据;
}
//清除已处理的数据
公共无效ClearPendingData()
{
m_数据移除(0);
}
}
////////////////////////////////////////////////////////////////////
//要修改的开放源代码库,以支持新设备
////////////////////////////////////////////////////////////////////
公共结构事件
{
//定义事件的公共结构
公共int类型;
公共字节[]数据;
// ...
}
//开放源代码库的假设非静态实现
//进行调整以支持新的设备类型
公共类OpenSourceLibWrapperClass
{
// ....
//添加设备引用以支持新设备
NewDeviceLib m_设备=空;
//添加NewDeviceLib的实例(必须是非静态的)
NewDeviceHandler m_NewDeviceHandler;
//所有连接设备的初始化
私有void InitNewDevice()
{
//添加以支持新的设备类型
m_device=NewDeviceLib.GetDevice();
如果(m_设备!=null)
{
m_device.SetDataCallback(m_newDeviceHandler);
}
}
//包装C函数库的现有方法
[DllImport(“DLL_NAME”,EntryPoint=“\u Init”)]
私有静态外部void_Init();
公共void Init()
{
//添加以初始化新设备
InitNewDevice();
//为其他设备调用外部C函数
_Init();
}
//包装C函数库的现有方法
//处理完所有挂起事件后返回0
[DllImport(“DLL_NAME”,EntryPoint=“\u PollEvent”)]
私有静态外部int_PollEvent(out Event DevEvent);
public int PollEvent(out Event DevEvent)
{
//添加以支持新设备
如果(m_设备!=null)
{
如果(m_newDeviceHandler.GetPendingData().Count>0)
{
DevEvent=新事件();
DevEvent.type=9999;
DevEvent.data=m_newDeviceHandler.GetPendingData()[0];
m_newDeviceHandler.ClearPendingData();
返回m_newDeviceHandler.GetPendingData().Count;
}
}
//其他设备事件的常规处理
返回PollEvent(out-DevEvent);
}
}
}
我是否应该继续,因为OpenSourceWrapperClass类必须保持静态?用实现所需接口的非静态类包装您的静态类,如下所示(您只提供了一个接口,因此在本例中只有一个接口): 比如说:
SetDataCallback(new SldDataHandler());
你的问题不清楚。你想写什么代码?我已经修改了我的帖子,加入了一个更详细的示例。
SetDataCallback(new SldDataHandler());