微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何创build从C#到C + +委托,将IEnumerable作为参数与SWIG的通过?

所以我有一个C + +代码

#ifdef WIN32 # undef CALLBACK # define CALLBACK __stdcall #else # define CALLBACK #endif #include <iostream> #include <vector> namespace OdeProxy { typedef std::vector< double > state_type; typedef void (CALLBACK *System)( const state_type &,state_type &,const double); typedef void (CALLBACK *Observer)( const state_type &,double); class Ode { public: state_type initialConditions; System system; Observer observer; double from; double to; double step; }; }

和.i文件

/* File : MyProject.i */ %module MyProject %{ #include "C++/OdeProxy.h" %} %include "std_vector.i" %include "C++/OdeProxy.h" %template(state_type) std::vector<double>; //// Delegate realated stuff //// %typemap(cstype) void (*)( const state_type &,const double) "SystemDelegate"; %typemap(imtype) void (*)( const state_type &,const double) "SystemDelegate"; %typemap(cstype) void (*)( const state_type &,double) "ObserverDelegate"; %typemap(imtype) void (*)( const state_type &,double) "ObserverDelegate";

我创造了这个线程的启发。 代码生成

然而,我不明白如何获得代码

将节点添加到树视图中的特定父节点(c#)

来自.NET的关于文件path的奇怪行为

了解Windows中的AppDomain

.NET锁性能计数器差异

如何在C#中使用Process.Start中的空格处理值

using OdeLibrary; namespace OdeTest { class Program { static void Main(string[] args) { //var lam = new OdeLibrary.SWIGTYPE_p_f_r_q_const__std__vector__double___double__void() var ode = new Ode{ from = 0,to = 10,initialConditions = new state_type(new[]{1,2,3}),step = 0.01,observer = (x,dxdt,t) => { return; } }; } } }

编译。 错误

Error Cannot convert lambda expression to type 'OdeLibrary.SWIGTYPE_p_f_r_q_const__std__vector__double___double__void' because it is not a delegate type

SWIGTYPE_p_f_r_q_const__std__vector__double___double__void看起来像这样:

/* ---------------------------------------------------------------------------- * This file was automatically generated by SWIG (http://www.swig.org). * Version 2.0.9 * * Do not make changes to this file unless you kNow what you are doing--modify * the SWIG interface file instead. * ----------------------------------------------------------------------------- */ namespace OdeLibrary { using System; using System.Runtime.InteropServices; public class SWIGTYPE_p_f_r_q_const__std__vector__double___double__void { private HandleRef swigCPtr; internal SWIGTYPE_p_f_r_q_const__std__vector__double___double__void(IntPtr cPtr,bool futureUse) { swigCPtr = new HandleRef(this,cPtr); } protected SWIGTYPE_p_f_r_q_const__std__vector__double___double__void() { swigCPtr = new HandleRef(null,IntPtr.Zero); } internal static HandleRef getCPtr(SWIGTYPE_p_f_r_q_const__std__vector__double___double__void obj) { return (obj == null) ? new HandleRef(null,IntPtr.Zero) : obj.swigCPtr; } } }

所以我不知道什么应该改变.i文件添加到C#生成的包装,以获得通过我的C#lambda到C ++类作为委托的能力?

CustomSettingProvider中的空引用仅在某些机器上

在winforms应用程序中dynamic设置图片框的图像?

禁用交换(分页文件)以确保一切都在内存中

如何asynchronous打开Windows中的文件

在C#3.0中的Windows本地用户帐户的身份validation

得到这个工作以下配置:

public class StateTypeCustomMarshaller : ICustomMarshaler { public static ICustomMarshaler GetInstance(string s) { return new StateTypeCustomMarshaller(); } public object MarshalNativetoManaged(IntPtr pNativeData) { return new state_type(pNativeData,false); } public IntPtr MarshalManagedToNative(object Managedobj) { throw new NotImplementedException(); } public void CleanUpNativeData(IntPtr pNativeData) { throw new NotImplementedException(); } public void CleanUpManagedData(object Managedobj) { } public int GetNativeDataSize() { throw new NotImplementedException(); } } public delegate void ObserverDelegate( [MarshalAs(UnmanagedType.CustomMarshaler,MarshalTypeRef = typeof(StateTypeCustomMarshaller))]state_type state,double d);

对应的.i文件是这样的:

/* File : MyProject.i */ %module MyProject %include "std_vector.i" %template(state_type) std::vector<double>; //// Delegate realated stuff //// %typemap(csin) void (*)(OdeProxy::state_type&,double) "$csinput"; %typemap(cstype) void (*)(OdeProxy::state_type&,double) "ConsoleApplication2.Helpers.ObserverDelegate"; %typemap(imtype) void (*)(OdeProxy::state_type&,double) "ConsoleApplication2.Helpers.ObserverDelegate"; %typemap(csvarout) void (*)(OdeProxy::state_type&,double) %{ get { return $imcall; } %} %{ #include "OdeProxy.h" %} %include "OdeProxy.h"

注意:我已经尝试了非常量引用状态类型,但是不断的引用也可以工作。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐