banboshi_V1/halftoneproject-master/Code/UI/LightProp.cs

66 lines
2.1 KiB
C#
Raw Permalink Normal View History

2023-10-31 13:19:29 +08:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Advantech.Motion;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using ProductionControl.UI.PropExtend;
using static ProductionControl.UI.UIAxis;
namespace ProductionControl.UI
{
//[TypeConverter(typeof(PropertySorter))] //此属性使类无法序列化和反序列化
public class LightProp
{
public LightProp(int portNum) {
PortNum = portNum;
}
[PropertyOrder(1), Browsable(true), Category("设备"), DisplayName("端口号"), Description("端口号"), ReadOnly(true), JsonIgnore]
public int PortNum { get;private set; }
//
[PropertyOrder(11), Browsable(true), Category("通道"), DisplayName("通道号"), Description("通道号")]
public int ChannelIndex { get; set; } = 1;
[PropertyOrder(12), Browsable(true), Category("数据"), DisplayName("亮度"), Description("亮度")]
public int DigitalValue { get; set; }
/// <summary>
/// 序列化
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public string serialize()
{
return JsonConvert.SerializeObject(this);
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public void deserialize(string json)
{
var o= JsonConvert.DeserializeObject<LightProp>(json);
Type type = o.GetType();
System.Reflection.PropertyInfo[] properties = type.GetProperties();
foreach (System.Reflection.PropertyInfo property in properties)
{
string name = property.Name;
if (!type.GetProperty(name).IsDefined(typeof(JsonIgnoreAttribute), true))
{
var value = property.GetValue(o);
this.GetType().GetProperty(name).SetValue(this, value);
}
}
}
}
}