ProtocolProxy.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using WCS.Core;
  2. using WCS.Entity.Protocol.DataStructure;
  3. using WCS.Entity.Protocol.Protocol.DataStructure;
  4. using WCS.WorkEngineering.Systems;
  5. namespace WCS.WorkEngineering
  6. {
  7. public class ProtocolProxy : ProtocolProxyBase
  8. {
  9. public static DataPack DataPack { get; set; } = new DataPack();
  10. public ProtocolProxy(Device dev, ProtocolInfo info, Type protocolType, World world) : base(dev, info, protocolType, world)
  11. {
  12. }
  13. protected override void DataChanged()
  14. {
  15. //var p = DataPack.GetType().GetProperties()
  16. // .Where(v => v.PropertyType.IsGenericType)
  17. // .Where(v =>
  18. // {
  19. // var t = v.PropertyType.GenericTypeArguments[0].GenericTypeArguments[0];
  20. // return GetProtocolType(t) == this.ProtocolType;
  21. // })
  22. // .FirstOrDefault();
  23. }
  24. Type GetProtocolType(Type source)
  25. {
  26. var t = source.GetInterfaces()
  27. .Where(v => v.GetInterfaces().Any(d => d.Name == "IProtocol")).FirstOrDefault();
  28. return t;
  29. }
  30. public object DictionaryToEntity(Type type, Dictionary<string, PlcItem> items)
  31. {
  32. var a = Activator.CreateInstance(type);
  33. foreach (var ty in type.GetProperties())
  34. {
  35. foreach (var item in items.Where(item => ty.Name == item.Key))
  36. {
  37. ty.SetValue(a, item.Value.Value);
  38. break;
  39. }
  40. }
  41. return a;
  42. }
  43. }
  44. }