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 int GetPacketID(IPacket oPacket) { if (oPacket.PacketID != 0) { return(oPacket.PacketID); } else { object[] objs = GetType().GetCustomAttributes(typeof(ProtocolAttribute), false); if (objs.Length > 0) { ProtocolAttribute oAttr = objs[0] as ProtocolAttribute; oPacket.PacketID = oAttr.ID; } return(oPacket.PacketID); } }