Device.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace WCS.Core
  8. {
  9. public class EntityEx<T>
  10. {
  11. public T Entity { get; private set; }
  12. public EntityEx(T ent)
  13. {
  14. this.Entity = ent;
  15. }
  16. }
  17. public class Device
  18. {
  19. public string Code { get; set; } = "";
  20. public World World { get; set; }
  21. ConcurrentDictionary<Type, object> ProtocolObjs = new ConcurrentDictionary<Type, object>();
  22. public bool HasProtocol(Type type)
  23. {
  24. return Protocols.HasProtocol(type, Code);
  25. //return ProtocolObjs.ContainsKey(type);
  26. }
  27. public bool HasProtocol<T>()
  28. {
  29. return HasProtocol(typeof(T));
  30. }
  31. public T Protocol<T>()
  32. {
  33. return (T)Protocol(typeof(T));
  34. }
  35. public object Protocol(Type protocolType)
  36. {
  37. if (!ProtocolObjs.ContainsKey(protocolType))
  38. {
  39. var type = typeof(Generator<,>);
  40. type = type.MakeGenericType(protocolType, Configs.ProtocolProxyBaseType);
  41. var m = type.GetMethod("Create", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
  42. var info = Protocols.Get(protocolType, Code);
  43. var obj = m.Invoke(null, new object[] { new object[] { this, info, protocolType } });
  44. ProtocolObjs[protocolType] = obj;
  45. }
  46. return ProtocolObjs[protocolType];
  47. }
  48. public override string ToString()
  49. {
  50. return Code;
  51. }
  52. }
  53. public class Device<T> : EntityEx<Device>
  54. {
  55. public T Data { get; set; }
  56. public Device(Device device) : base(device)
  57. {
  58. Data = Entity.Protocol<T>();
  59. }
  60. public override string ToString()
  61. {
  62. return Entity.Code;
  63. }
  64. }
  65. public class Device<T, T2> : Device<T>
  66. {
  67. public T2 Data2 { get; set; }
  68. public Device(Device device) : base(device)
  69. {
  70. Data2 = Entity.Protocol<T2>();
  71. }
  72. }
  73. public class Device<T, T2, T3> : Device<T, T2>
  74. {
  75. public T3 Data3 { get; set; }
  76. public Device(Device device) : base(device)
  77. {
  78. Data3 = Entity.Protocol<T3>();
  79. }
  80. }
  81. public class Device<T, T2, T3, T4> : Device<T, T2, T3>
  82. {
  83. public T4 Data4 { get; set; }
  84. public Device(Device device) : base(device)
  85. {
  86. Data4 = Entity.Protocol<T4>();
  87. }
  88. }
  89. public class Device<T, T2, T3, T4, T5> : Device<T, T2, T3, T4>
  90. {
  91. public T5 Data5 { get; set; }
  92. public Device(Device device) : base(device)
  93. {
  94. Data5 = Entity.Protocol<T5>();
  95. }
  96. }
  97. public class Device<T, T2, T3, T4, T5, T6> : Device<T, T2, T3, T4, T5>
  98. {
  99. public T6 Data6 { get; set; }
  100. public Device(Device device) : base(device)
  101. {
  102. Data6 = Entity.Protocol<T6>();
  103. }
  104. }
  105. }