Is it possible to add code to an existing method when using Swig to build a C# wrapper for C++ code?

前端 未结 1 1815
甜味超标
甜味超标 2021-01-25 14:32

When using Swig to wrap C++ code, it is possible to add methods to the native C++ type using %extend. It is possible to add methods to the C# wrapper class using

1条回答
  •  [愿得一人]
    2021-01-25 14:59

    In order to write your own version you're going to need to make sure it doesn't clash with the default one. I think the easiest way to do this would be to make ItemChanged private/protected using %csmethodmodifiers and %rename to hide the default wrapped version that gets generated. Once it's hidden.

    You can then safely write your own version of ItemChanged using a cscode typemap that firstly calls the private version and then calls your additional code as you desire.

    This is by far the cleanest way of solving the problem - the only way you're able to inject code directly into the generated code is as part of the argument/return value marshalling - setting up the arguments that get passed in, cleaning up after or handling the return value. Messing with one of those typemaps to inject some code would be fairly messy.

    0 讨论(0)
提交回复
热议问题