版博士V2.0程序
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

66 рядки
2.2 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Xml.Linq;
  8. using Advantech.Motion;
  9. using Newtonsoft.Json;
  10. using Newtonsoft.Json.Serialization;
  11. using ProductionControl.UI.PropExtend;
  12. using static ProductionControl.UI.UIAxis;
  13. namespace ProductionControl.UI
  14. {
  15. //[TypeConverter(typeof(PropertySorter))] //此属性使类无法序列化和反序列化
  16. public class IOCardProp
  17. {
  18. public IOCardProp(string deviceNum) {
  19. DeviceNum = deviceNum;
  20. }
  21. [PropertyOrder(1), Browsable(true), Category("设备"), DisplayName("设备号"), Description("设备号"), ReadOnly(true), JsonIgnore]
  22. public string DeviceNum { get;private set; }
  23. //
  24. [PropertyOrder(11), Browsable(true), Category("DI"), DisplayName("输入"), Description("输入"), ReadOnly(true), JsonIgnore]
  25. public byte[] IN { get; set; }
  26. [PropertyOrder(12), Browsable(true), Category("DO"), DisplayName("输出"), Description("输出")]
  27. public byte[] OUT { get; set; }
  28. /// <summary>
  29. /// 序列化
  30. /// </summary>
  31. /// <param name="obj"></param>
  32. /// <returns></returns>
  33. public string serialize()
  34. {
  35. return JsonConvert.SerializeObject(this);
  36. }
  37. /// <summary>
  38. /// 反序列化
  39. /// </summary>
  40. /// <param name="json"></param>
  41. /// <returns></returns>
  42. public void deserialize(string json)
  43. {
  44. var o= JsonConvert.DeserializeObject<IOCardProp>(json);
  45. Type type = o.GetType();
  46. System.Reflection.PropertyInfo[] properties = type.GetProperties();
  47. foreach (System.Reflection.PropertyInfo property in properties)
  48. {
  49. string name = property.Name;
  50. if (!type.GetProperty(name).IsDefined(typeof(JsonIgnoreAttribute), true))
  51. {
  52. var value = property.GetValue(o);
  53. this.GetType().GetProperty(name).SetValue(this, value);
  54. }
  55. }
  56. }
  57. }
  58. }