I'm using Lidgren and for every new type of message I make, I end up writing the same kind of code. I'm creating an instance of NetOutgoingMessage
, running various assignment calls on it, then sending it when I'm done. Creation and send are the same, so I want to write a wrapper to do this for me, but it's a sealed
class and it's not IDisposable
. What I'm doing is something like:
NetOutgoingMessage om = server.CreateMessage();
om.Write(messageType);
om.Write(data1);
om.Write(data2);
server.SendMessage(om, server.Connections, NetDeliveryMethod.UnreliableSequenced, 0);
And I want to do something like:
using(AutoNetOutgoingMessage om(server, messageType, NetDeliveryMethod.UnreliableSequenced))
{
om.Write(data1);
om.Write(data2);
}
Obviously I can't do using
so is there another common way if implementing functionality like this? I'm not looking for an incredibly complicated solution, as this is just about maintainability for me, so I don't have a problem repeating my code for every message. But I am curious if there's a fun C# trick I don't know about for this.