Generic event?
-
I'm developing a plug-in system. Each plug-in can register to handle a packet. I'd like to have the manager have some sort of "generic event", so the plug-in would look something like this:
public class TestPlugin : Plugin
{
public override void Register(PluginManager manager)
{
manager.OnPacket<Packets.SomePacket> += new PacketEventHandler(manager_OnPacketSomePacket);
}public void manager\_OnPacketSomePacket(PluginManager manager, PacketEventArgs e) { }
}
I could do it several other ways, of course, but the reason i'm looking for something like this is because if it's an event Visual Studio can generate a method stub, which is a useful functionality, which i'd definitely appreciate when i'm actually coding plug-ins. I can't make a separate event for each packet because there are ~300 different kinds of packets that could be received. Is there a way to do this, or an alternative that would keep the method-stub functionality I love so much? Thanks in advance.
-
I'm developing a plug-in system. Each plug-in can register to handle a packet. I'd like to have the manager have some sort of "generic event", so the plug-in would look something like this:
public class TestPlugin : Plugin
{
public override void Register(PluginManager manager)
{
manager.OnPacket<Packets.SomePacket> += new PacketEventHandler(manager_OnPacketSomePacket);
}public void manager\_OnPacketSomePacket(PluginManager manager, PacketEventArgs e) { }
}
I could do it several other ways, of course, but the reason i'm looking for something like this is because if it's an event Visual Studio can generate a method stub, which is a useful functionality, which i'd definitely appreciate when i'm actually coding plug-ins. I can't make a separate event for each packet because there are ~300 different kinds of packets that could be received. Is there a way to do this, or an alternative that would keep the method-stub functionality I love so much? Thanks in advance.
Have you tried it?
-
Have you tried it?
-
I'm developing a plug-in system. Each plug-in can register to handle a packet. I'd like to have the manager have some sort of "generic event", so the plug-in would look something like this:
public class TestPlugin : Plugin
{
public override void Register(PluginManager manager)
{
manager.OnPacket<Packets.SomePacket> += new PacketEventHandler(manager_OnPacketSomePacket);
}public void manager\_OnPacketSomePacket(PluginManager manager, PacketEventArgs e) { }
}
I could do it several other ways, of course, but the reason i'm looking for something like this is because if it's an event Visual Studio can generate a method stub, which is a useful functionality, which i'd definitely appreciate when i'm actually coding plug-ins. I can't make a separate event for each packet because there are ~300 different kinds of packets that could be received. Is there a way to do this, or an alternative that would keep the method-stub functionality I love so much? Thanks in advance.
My assumption is that the answer for this is that you cannot have a generic event for the same reason you cannot have a generic field in a non-generic class (or of a generic type that is not a type parameter for the class containing the field). If this was allowed, your class would have to vary in size depending on how many different events were used by external code, which of course is not allowed. The best option that comes to mind is making all of the packet classes can inherit from some base packet class. Then you can make the event use that base class and have the handlers only process packets of the expected type.
-
My assumption is that the answer for this is that you cannot have a generic event for the same reason you cannot have a generic field in a non-generic class (or of a generic type that is not a type parameter for the class containing the field). If this was allowed, your class would have to vary in size depending on how many different events were used by external code, which of course is not allowed. The best option that comes to mind is making all of the packet classes can inherit from some base packet class. Then you can make the event use that base class and have the handlers only process packets of the expected type.
That's disappointing. Packets is actually an enum of type-codes for each packet. Then the plug-ins parse the data (which is fed through a PacketEventArgs class) based on the type code. It seems about the only option would be to have a giant switch statement of packets, or a manager.Register(Packets.SomePacket, MyHandler) function. I think i'll end up going the latter route, a gigantic switch statement just doesn't appeal to me. Thanks for your help.
-
That's disappointing. Packets is actually an enum of type-codes for each packet. Then the plug-ins parse the data (which is fed through a PacketEventArgs class) based on the type code. It seems about the only option would be to have a giant switch statement of packets, or a manager.Register(Packets.SomePacket, MyHandler) function. I think i'll end up going the latter route, a gigantic switch statement just doesn't appeal to me. Thanks for your help.
I had assumed you had a set of classes since generics would not accept an enum value as the type parameter (since it is a constant and not a type). You could still take the approach of letting the handlers receive all events and only process the ones they care about. Something like:
void PacketProcessor(object sender, PacketEventArgs e)
{
if (e.PacketType != Packets.SomePacket) return;//do processing on SomePacket data here
}
This has the advantage of making it obvious to the later reader of the code which packet is being handled by this function, but has the disadvantage all packet handlers will get called for all packets.