public PacketFactory() { CreatorTable = new Dictionary <int, IPacketCreator>(); Type[] oTypes = ReflUtils.GetAllTypes(); foreach (var nType in oTypes) { if (!nType.IsClass) { continue; } Type oInterfaceType = nType.GetInterface(typeof(IPacketCreator).Name); if (oInterfaceType == typeof(IPacketCreator)) { PacketAttribute[] attributes = (PacketAttribute[])nType.GetCustomAttributes(typeof(PacketAttribute), false); if (attributes != null && attributes.Length > 0) { PacketAttribute attribute = attributes[0]; if (CreatorTable.ContainsKey(attribute.PacketID)) { App.Error("协议结构类已存在!PacketID:{0}", attribute.PacketID); continue; } CreatorTable.Add(attribute.PacketID, (IPacketCreator)Activator.CreateInstance(nType)); } } } }
private void AssemblyProcesser() { Type[] oTypes = ReflUtils.GetAllTypes(); foreach (var type in oTypes) { if (!type.IsClass) { continue; } if (type.BaseType == null) { continue; } if (type.BaseType.Name == typeof(Protocol <>).Name) { Type[] genericTypes = type.BaseType.GenericTypeArguments; if (genericTypes.Length > 0) { Type packetType = genericTypes[0]; PacketAttribute[] attributes = (PacketAttribute[])packetType.GetCustomAttributes(typeof(PacketAttribute), false); if (attributes.Length > 0) { PacketAttribute attr = attributes[0]; if (Processers.ContainsKey(attr.PacketID)) { App.Error("协议结构处理类已存在!PacketID:{0}", attr.PacketID); continue; } Processers.Add(attr.PacketID, (IProtocol)Activator.CreateInstance(type)); } } } } }
private void AssemblyProcesser() { Type[] oTypes = ReflUtils.GetAllTypes(); foreach (var nType in oTypes) { if (!nType.IsClass) { continue; } Type oInterfaceType = nType.GetInterface(typeof(IPacketProcesser).Name); if (oInterfaceType == typeof(IPacketProcesser)) { PacketAttribute[] attributes = (PacketAttribute[])nType.GetCustomAttributes(typeof(PacketAttribute), false); if (attributes != null && attributes.Length > 0) { PacketAttribute attr = attributes[0]; if (Processers.ContainsKey(attr.PacketID)) { App.Logger.Error("协议结构处理类已存在!PacketID:{0}", attr.PacketID); continue; } Processers.Add(attr.PacketID, (IPacketProcesser)Activator.CreateInstance(nType)); } } } }
public PreloadMgr() { Preloads = new List <PreloadCtrl>(); Type[] oTypes = ReflUtils.GetAllTypes((oType) => { bool isSucess = false; if (oType.IsClass && oType.GetInterface(typeof(IPreload).Name) == typeof(IPreload)) { isSucess = true; } return(isSucess); }); for (int i = 0; i < oTypes.Length; i++) { Type oType = oTypes[i]; IPreload oPreload = Activator.CreateInstance(oType) as IPreload; object[] oAttrs = oType.GetCustomAttributes(typeof(PreloadSortAttribute), false); PreloadCtrl oPreloadCtrl = new PreloadCtrl(); oPreloadCtrl.SortID = 99; oPreloadCtrl.IPreload = oPreload; if (oAttrs.Length > 0) { oPreloadCtrl.SortID = (oAttrs[0] as PreloadSortAttribute).SortID; } Preloads.Add(oPreloadCtrl); } Preloads.Sort((x, y) => x.SortID.CompareTo(y.SortID)); IsDone = true; }
private static object Construct(Type type, params object[] args) { return(ReflUtils.GetConstructor(type, args).Invoke(args)); }