-1
public class Event
{
    private TimeSpan m_Offset = TimeSpan.Zero;              
    private TimeSpan m_Duration = TimeSpan.Zero;            
    private TimeSpan m_Countdown = TimeSpan.Zero;           
    private int m_OffsetFromUTC = -8;                       
    DateTime m_EventStart, m_EventEnd;                      
    private Server.Timer m_Timer;                           
    private bool m_Calledstart = false;                     
    private Item m_item;                                    
    private object m_context;                               // optional caller context object
    private Action<object> m_startedCallback;               // optional caller event started function
    private Action<object> m_endedCallback;                 // optional caller event ended function

    public Event(Item you, object context = null, Action<object> startedCallback = null, Action<object> endedCallback = null)
    {
        m_item = you;
        m_context = context;
        m_startedCallback = startedCallback;
        m_endedCallback = endedCallback;
    }
    public void Serialize(GenericWriter writer)
    {
        writer.Write((int)1);   // version
        writer.Write(DateTime.Now);
        writer.Write(m_Duration);
        writer.Write(m_Calledstart);
        writer.Write(m_OffsetFromUTC);
        writer.Write(m_Offset);
    }
    public void Deserialize(GenericReader reader)
    {
        int version = reader.ReadInt();
        switch (version)
        {
            case 1:
                {
                    DateTime temp = reader.ReadDateTime();
                    m_Countdown = DateTime.Now - temp;
                    m_Duration = reader.ReadTimeSpan();
                    m_Calledstart = reader.ReadBool();
                    m_OffsetFromUTC = reader.ReadInt();
                    m_Offset = reader.ReadTimeSpan();
                    break;
                }
        }
    }
}

What I need to do here is find a way to Serialize m_startedCallback and m_endedCallback, and then Deserialize them back into a usable form. I looked briefly at GetMethodInfo but it wasn't clear it would work on a variable, and even then, I don't know how to convert that back to a ligament function reference once I Deserialize.

Any help here would be appriciated.

Level 42
  • 400
  • 2
  • 7
  • 6
    Serialize them to _what_ exactly? To text? That's not going to make much sense. Say I provide a callback which interfaces with a machine which sends the value passed to the `Action` using smoke signals.... how would that be "serialized" exactly? (Silly example, but you get the idea) – Jamiec Oct 14 '22 at 16:42
  • I'm serializing to a binary stream - saving game state. When the server restarts, it reads in all this 'saved game state' – Level 42 Oct 14 '22 at 16:54
  • This is not a good idea. Lots of good information in https://stackoverflow.com/questions/49138328/how-come-binaryformatter-can-serialize-an-action-but-json-net-cannot – BurnsBA Oct 14 '22 at 16:55
  • BinarySerialization would work but, depending on what framework your using it might not be available – JoshBerke Oct 14 '22 at 17:09
  • Framework 6, C# version 10. – Level 42 Oct 14 '22 at 17:12

1 Answers1

0

The approach I was taking of trying to serialize the callback itself seems misguided. The approach I've adopted is far simpler. In my constructor I simply salt away the name of the callback:

m_startedCallbackName = startedCallback.Method.Name;

Serialization is then easy because you just write/read the string name. Then, when you wish to call the callback, you just do the following:

MethodInfo mis = m_item.GetType().GetMethod(m_startedCallbackName);
mis.Invoke(m_item, new object[] { m_context });
Level 42
  • 400
  • 2
  • 7