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

When using Swig to port code to C ++, you can add methods to your own C ++ type using %extend . add methods to a C # shell class using %typemap(cscode) .

Where the method already exists in the wrapper class, is there a way to add additional lines of code?

For example, my C # shell method looks like this:

  public void ItemChanged(CollectionObject collectionObject, string propertyName) { mynamespacePINVOKE.mynamespace_DataObjectCollection_ItemChanged(swigCPtr, CollectionObject.getCPtr(collectionObject), propertyName); if (mynamespacePINVOKE.SWIGPendingException.Pending) throw mynamespacePINVOKE.SWIGPendingException.Retrieve(); } 

I would like to add a line of code:

  public void ItemChanged(CollectionObject collectionObject, string propertyName) { mynamespacePINVOKE.mynamespace_DataObjectCollection_ItemChanged(swigCPtr, CollectionObject.getCPtr(collectionObject), propertyName); if (mynamespacePINVOKE.SWIGPendingException.Pending) throw mynamespacePINVOKE.SWIGPendingException.Retrieve(); **insert my line of code here** } 
+1
source share
1 answer

To write your version, you will need to make sure that it does not encounter a default. I think the easiest way to do this is to make ItemChanged private / protected with %csmethodmodifiers and %rename to hide the default version that will be generated. As soon as he hides.

You can then safely write your own version of ItemChanged using a generic cscode card that first calls up the personal version and then calls up your extra code as you wish.

This is by far the cleanest way to solve the problem - the only way you can enter the code directly into the generated code is part of the marshalling of the argument / return value - setting up the arguments that are passed, clearing after, or handling the return value. Messing with one of these character types to enter some code will be pretty messy.

+1
source

Source: https://habr.com/ru/post/1207047/


All Articles