private void AssemblyPacket() { List <Type> typeList = new List <Type>(); Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); for (int i = 0; i < assemblies.Length; i++) { typeList.AddRange(assemblies[i].GetTypes()); } Type[] types = typeList.ToArray(); for (int i = 0; i < types.Length; i++) { Type type = types[i]; object[] objs = type.GetCustomAttributes(typeof(ProtocolAttribute), false); if (objs.Length > 0) { // 如果包含包属性,则创建对象 object obj = Activator.CreateInstance(type); ProtocolAttribute oAttr = objs[0] as ProtocolAttribute; if (oAttr.Type == ProcessType.Receive) { IPacket oPacket = obj as IPacket; PacketTable.Add(oAttr.ID, oPacket); } else if (oAttr.Type == ProcessType.Process) { IProtocolProcess oProtocol = obj as IProtocolProcess; ProtocolTable.Add(oAttr.ID, oProtocol); } } } }
public void Tick() { while (Packets.Count > 0) { IPacket oPacket = Packets.Dequeue(); IProtocolProcess protocolProcess = null; if (ProtocolTable.TryGetValue(oPacket.PacketID, out protocolProcess)) { try { protocolProcess.ToProcess(oPacket); } catch (Exception e) { Console.WriteLine(e); } } } }