private TransportProtocol() { this.container = new Dictionary <string, TransportProtocolContainer>(); Assembly assembly = Assembly.GetExecutingAssembly(); IEnumerable <Type> types = assembly.GetTypes().Where(c => c.GetInterface(typeof(ITransportProtocol).Name) != null && c.GetCustomAttributes(typeof(ATransportProtocol), false).Length == 1); foreach (Type type in types) { ATransportProtocol attribute = (ATransportProtocol)(type.GetCustomAttributes(typeof(ATransportProtocol), false)[0]); FieldInfo instanceField = type.GetField("instance", BindingFlags.NonPublic | BindingFlags.Static); if (instanceField != null) { TransportProtocolContainer protocolContainer = null; if (!this.container.ContainsKey(attribute.Name)) { protocolContainer = new TransportProtocolContainer(); this.container.Add(attribute.Name, protocolContainer); } else { protocolContainer = this.container[attribute.Name]; } ITransportProtocol protocol = (ITransportProtocol)instanceField.GetValue(null); foreach (byte version in attribute.Versions) { //Log.Add("ver: " + version); protocolContainer.Add(version, protocol); } } } }
private TransportProtocol() { this.container = new Dictionary<string, TransportProtocolContainer>(); Assembly assembly = Assembly.GetExecutingAssembly(); IEnumerable<Type> types = assembly.GetTypes().Where(c => c.GetInterface(typeof(ITransportProtocol).Name) != null && c.GetCustomAttributes(typeof(ATransportProtocol), false).Length == 1); foreach(Type type in types) { ATransportProtocol attribute = (ATransportProtocol)(type.GetCustomAttributes(typeof(ATransportProtocol), false)[0]); FieldInfo instanceField = type.GetField("instance", BindingFlags.NonPublic | BindingFlags.Static); if(instanceField != null) { TransportProtocolContainer protocolContainer = null; if(!this.container.ContainsKey(attribute.Name)) { protocolContainer = new TransportProtocolContainer(); this.container.Add(attribute.Name, protocolContainer); } else { protocolContainer = this.container[attribute.Name]; } ITransportProtocol protocol = (ITransportProtocol)instanceField.GetValue(null); foreach(byte version in attribute.Versions) { //Log.Add("ver: " + version); protocolContainer.Add(version, protocol); } } } }