8

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;
}
Krizz
  • 11,362
  • 1
  • 30
  • 43
Jeff
  • 5,746
  • 4
  • 33
  • 40

2 Answers2

5

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.

Community
  • 1
  • 1
Krizz
  • 11,362
  • 1
  • 30
  • 43
  • Nope. Through debugging I've verified I am getting the event back correctly. Just mono_event_get_raise_method returns null. get_add_method and get_remove_method return values. – Jeff Jan 31 '12 at 22:55
4

Krizz's answer is the most complete. This is how I fixed my code to work how I would "expect".

I changed the C# side to:

public delegate void MyHandler(uint aEntityId);
public class SimpleComponent: NativeComponent
{
    public event MyHandler OnEnter;
    public event MyHandler OnExit;

    protected void CallOnEnter(uint aEntityId)
    {
        if (OnEnter != null)
            OnEnter(aEntityId);
    }

    protected void CallOnExit(uint aEntityId)
    {
        if (OnExit!= null)
            OnExit(aEntityId);
    }
}

Then grabbed the mono method with

raiseMethod = mono_class_get_method_from_name(klass, "CallOnEnter", 1);
Jeff
  • 5,746
  • 4
  • 33
  • 40
  • Bah, beat me to it. This is pretty much the convention in .NET anyway. The docs on events use it ([Raising an Event](http://msdn.microsoft.com/en-us/library/wkzf914z.aspx) and the related [How To](http://msdn.microsoft.com/en-us/library/9aackb16.aspx)), and it's used all over the place in the framework classes too. Minor aside: the naming conventions would make `Entering` and `Exiting` the "correct" names for the event fields, and `OnEntering()` / `OnExiting` should be used for the methods raising them. – millimoose Jan 31 '12 at 23:30