[Mono-dev] Func parameter in embedded mono
Robert Jordan
robertj at gmx.net
Wed Mar 28 21:01:14 UTC 2018
Hi,
On 28.03.2018 15:36, pierre wrote:
> I am looking for info on how I can setup a callback in an embedded mono?
>
> so, I can define in C#, something like:
>
> Class Widget {
>
> private IntPtr _native;
>
> [MethodImplAttribute(MethodImplOptions.InternalCall)]
>
> static extern private void mono_set_callback(IntPtr widget,
> Func<Widget, Bool> callback);
>
> public void setCallback(Func<Widget, Bool>callback)
>
> {
>
> mono_set_callback(_native, callback);
>
> }
>
> }
>
> Is that correct?
>
> what will be my C function?
>
> void mono_set_callback(mono_widget_object *widget, ???? *callback) { ... }
>
> and how can I call the callback from C? There is mono_runtime_invoke but
> it need an object (or NULL if static) and a method...
>
The callback argument is a MonoObject* because it's a delegate.
It already encapsulates the "this" pointer.
Example:
gint32 static_handle
void mono_set_callback(mono_widget_object *widget, MonoObject *callback)
{
/* keep delegate alive! */
static_handle = mono_gchandle_new (callback, FALSE);
}
void invoke_callback (...)
{
/* get object from handle */
MonoObject *del = mono_gchandle_get_target (static_handle);
MonoMethod *invoke = mono_get_delegate_invoke (
mono_object_get_class (del));
mono_runtime_invoke(invoke, del, ...)
}
Robert
More information about the Mono-devel-list
mailing list