No. There is no Marshal class method for this. Like all code that is dynamically generated, the thunk generated by this method is associated with the AppDomain and is unloaded when the AppDomain is unloaded.
Note that this does not apply to the delegate object, it follows the usual rules for garbage collection. And you have to be careful, the trick does not keep him alive. What is the error in your code, the delegate can be assembled when the native code is busy. You need to add this line of code to the end of the method:
GC::KeepAlive(callback);
With the assumption that the callback will only be executed as long as the Execute () method is executed. If unmanaged code stores a function pointer outside of this method call, you need to save the delegation object somewhere so that it is right.
source share