简体   繁体   中英

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

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;
}

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM