private void AssemblyPacket() { Type[] types = ZCommUtil.GetTypes(); 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); } } } }
private void PopulateProtocols() { foreach (Type type in this.GetType().Assembly.GetTypes()) { if (type.IsAbstract) { continue; } if (type.IsSubclassOf(typeof(AbstractProtocol))) { ProtocolAttribute protocolAttribute = (ProtocolAttribute)type.GetCustomAttributes(typeof(ProtocolAttribute), false)[0]; IProtocol protocol = (IProtocol)Activator.CreateInstance(type, false); this.Protocols.Add(protocolAttribute.Protocol, protocol); } } }
protected override ProtocolAttribute GetProtocolAttribute(TypeReference type) { CustomAttribute attrib; if (!SharedStatic.TryGetAttributeImpl (type.Resolve (), Foundation, StringConstants.ProtocolAttribute, out attrib)) return null; if (!attrib.HasProperties) return new ProtocolAttribute (); var rv = new ProtocolAttribute (); foreach (var prop in attrib.Properties) { switch (prop.Name) { case "Name": rv.Name = (string) prop.Argument.Value; break; case "WrapperType": rv.WrapperType = (TypeDefinition) prop.Argument.Value; break; case "IsInformal": rv.IsInformal = (bool) prop.Argument.Value; break; default: throw ErrorHelper.CreateError (4147, "Invalid {0} found on '{1}'. Please file a bug report at http://bugzilla.xamarin.com", "ProtocolAttribute", type.FullName); } } return rv; }
/// <summary> /// 合并特性 /// </summary> /// <param name="atts"></param> public void Merge(ProtocolAttribute[] atts) { if (atts == null || atts.Length < 1) return; // 从设置过的特性来更新标识信息 foreach (ProtocolAttribute item in atts) { item.MergeTo(this); } }