Delegates in SWIG - C#
( complete duplicate of http://old.nabble.com/C%2B%2B-pointer-to-method-as-parameter-to-C--td17645155.html , but couldn't make the proposed macro work)
I've got the following C++ function (simplified) :
InputPort addInputPort(void(*callback)(InputPort));
Problem : the signature of the generated C# function is :
public InputPort addInputPort(SWIGTYPE_p_f__InputPort____void callback)
SWIGTYPE_p_f__InputP开发者_高级运维ort____void
is not a delegate (and has no public constructor anyway), so I can't use addInputPort.
How do I tell SWIG to use a delegate instead ? If the solution involves %typemap, please be extra patient with me...
The solution involves a %typemap. The effect you are seeing is the default useless mapping for unknown types. In your case, the unknown type is the function type void *(InputPort)
.
In fact, all of SWIG's translation is based on %typemaps which have already been written and live in standard SWIG libraries. You can investigate the initial %typemaps for C# in this Subversion location.
In particular, look for SWIGStringHelper
in csharphead.swg. That code maps a delegate to a C callback. The trick is to add a helper callback implementation in the SWIG module.
精彩评论