using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics.Tracing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WCS.Core { public class Protocols { protected static ConcurrentDictionary> ItemOfType = new ConcurrentDictionary>(); public static bool HasProtocol(Type type, string code) { return ItemOfType[type].ContainsKey(code); } public static ProtocolInfo Get(Type type, string code) { ItemOfType[type].TryGetValue(code, out var info); return info; } /// /// 实例化Device,并未实例化Protocol /// /// /// public static List Generate(World world) { List list = new List(); var gs = ItemOfType.SelectMany(v => v.Value.Select(d => new { code = d.Key, proto = d.Value, type = v.Key })).GroupBy(v=>v.code).ToList(); foreach (var g in gs) { var dev = new Device { Code = g.Key, World = world }; //foreach (var i in g) //{ // //dev.Protocol(i.type); //} list.Add(dev); } return list; } } public class Protocols : Protocols { static ConcurrentDictionary Items { get { if (!ItemOfType.TryGetValue(typeof(T), out var item)) { item = new ConcurrentDictionary(); ItemOfType[typeof(T)] = item; } return item; } } public static void Add(string code, ProtocolInfo info) { if (!Items.TryAdd(code, info)) throw new Exception($"Protocols<{typeof(T)}>.Add()添加了重复的设备号"); } public static ProtocolInfo Get(string code) { return Get(typeof(T), code); } } }