Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded Mono: How do you raise an event in C++?

Tags:

c++

c#

events

mono

I'm working on an application that's embedding Mono, and I'd like to raise an event from the C++ layer into the C# layer. Here's what I have:

 void* itr(NULL);
 MonoEvent* monoEvent;
 while(monoEvent= mono_class_get_events(klass, &itr))
 {
     if(0 == strcmp(eventName, mono_event_get_name(monoEvent)))
         raiseMethod = mono_event_get_raise_method(monoEvent);
 }

However, raiseMethod always comes back as NULL. Looking at the structure of the MonoEvent, it looks like the add and remove methods were populated, but not the raise? Is there something special I have to do to get this to work?

EDIT: If it matters, here's the (basic) form of the delegate, class, and events I'm using in the C# layer.

public delegate void MyHandler(uint id);
public class SimpleComponent : NativeComponent
{
    public event MyHandler OnEnter;
    public event MyHandler OnExit;
}
like image 528
Jeff Avatar asked Jan 31 '12 22:01

Jeff


1 Answers

May the event be defined in parent class? If so you need to traverse up the class hierarchy with something like the following:

MonoEvent* monoEvent;
while (klass)
{
    void* itr = NULL;
    while(monoEvent= mono_class_get_events(klass, &itr))
    {
       if(0 == strcmp(eventName, mono_event_get_name(monoEvent)))
         raiseMethod = mono_event_get_raise_method(monoEvent);
   }
   klass = mono_class_get_parent(klass);
}

EDIT after comment and re-reading question:

It is normal that the raise method for event is NULL.

This method usually returns null for events declared with the C# event keyword or the Visual Basic Event keyword. This is because the C# and Visual Basic compilers do not generate such a method by default.

(source)

I am afraid it may be hard to fire an event of a class. Because it is actually breaking the concept of events in .NET - which says that the class itself can only fire its own Event. Actually, even from C# it is hard to raise the event of other class.

Conceptually, events are pair of add_handler and remove_handler methods where you specify delegates to be called when event's circumstances occur. It is up to class how it implements events. Technically, it is just a private delegate field, AFAIK. You may try to locate it.

I am not sure if it is a proper approach, but one of the answers in How do I raise an event via reflection in .NET/C#? describes how to raise event using reflection. You might attempt to convert it into mono_class / mono_field calls, etc.

like image 144
Krizz Avatar answered Sep 20 '22 08:09

Krizz