/// <summary>
/// Register a handler for {{ method_name }}.
/// </summary>
public int {{ method_name }}({% if metadata_params %}{{ metadata_params }}, {% endif %}Delegate handler) {
var handle = GCHandle.Alloc(handler, GCHandleType.Normal);
IntPtr ctx = GCHandle.ToIntPtr(handle);
int result = NativeMethods.{{ native_method }}(
_handle,
_handlerCallback,
ctx{{ arg_lines }}
);
if (result == 0) {
// Keep the GCHandle alive for the lifetime of the registration
lock (_registeredCallbacks) {
_registeredCallbacks[ctx] = handle;
}
} else {
handle.Free();
}
return result;
}