123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- using Newtonsoft.Json;
- using Newtonsoft.Json.Converters;
- using Newtonsoft.Json.Linq;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Reflection;
- using WCS.Entity.Protocol;
- namespace WCS_Client
- {
- /// <summary>
- /// 扩展.json序列反序列化
- /// </summary>
- public static partial class JsonExtensions
- {
- /// <summary>
- /// 转成json对象
- /// </summary>
- /// <param name="Json">json字串</param>
- /// <returns></returns>
- public static object ToJson(this string Json)
- {
- return Json == null ? null : JsonConvert.DeserializeObject(Json);
- }
- /// <summary>
- /// 转成json字串
- /// </summary>
- /// <param name="obj">对象</param>
- /// <returns></returns>
- public static string ToJson(this object obj)
- {
- var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
- return JsonConvert.SerializeObject(obj, timeConverter);
- }
- /// <summary>
- /// 转成json字串
- /// </summary>
- /// <param name="obj">对象</param>
- /// <param name="datetimeformats">时间格式化</param>
- /// <returns></returns>
- public static string ToJson(this object obj, string datetimeformats)
- {
- var timeConverter = new IsoDateTimeConverter { DateTimeFormat = datetimeformats };
- return JsonConvert.SerializeObject(obj, timeConverter);
- }
- /// <summary>
- /// 字串反序列化成指定对象实体
- /// </summary>
- /// <typeparam name="T">实体类型</typeparam>
- /// <param name="Json">字串</param>
- /// <returns></returns>
- public static T ToObject<T>(this string Json)
- {
- return Json == null ? default(T) : JsonConvert.DeserializeObject<T>(Json);
- }
- /// <summary>
- /// 字串反序列化成指定对象实体(列表)
- /// </summary>
- /// <typeparam name="T">实体类型</typeparam>
- /// <param name="Json">字串</param>
- /// <returns></returns>
- public static List<T> ToList<T>(this string Json)
- {
- return Json == null ? null : JsonConvert.DeserializeObject<List<T>>(Json);
- }
- /// <summary>
- /// 字串反序列化成DataTable
- /// </summary>
- /// <param name="Json">字串</param>
- /// <returns></returns>
- public static DataTable ToTable(this string Json)
- {
- return Json == null ? null : JsonConvert.DeserializeObject<DataTable>(Json);
- }
- /// <summary>
- /// 字串反序列化成linq对象
- /// </summary>
- /// <param name="Json">字串</param>
- /// <returns></returns>
- public static JObject ToJObject(this string Json)
- {
- return Json == null ? JObject.Parse("{}") : JObject.Parse(Json.Replace(" ", ""));
- }
- public static DataTable GetAttributesDataTable(this SCData obj)
- {
- var tb = new DataTable(obj.GetType().Name);
- tb.Columns.Add("信号说明", typeof(string));
- tb.Columns.Add("信号名称", typeof(string));
- tb.Columns.Add("信号值", typeof(string));
- foreach (PropertyInfo pi in obj.D521.GetType().GetProperties())
- {
- AttributeCollection attributes = TypeDescriptor.GetProperties(obj.D521.GetType())[pi.Name].Attributes;
- DescriptionAttribute myAttribute = (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];
- string describle = myAttribute.Description;
- string proname = pi.Name;
- var abc = pi.GetValue(obj.D521, null);
- switch (proname)
- {
- default:
- break;
- }
- var values = new object[3];
- values[0] = describle;
- values[1] = proname;
- values[2] = abc;
- tb.Rows.Add(values);
- }
- foreach (PropertyInfo pi in obj.D520.GetType().GetProperties())
- {
- AttributeCollection attributes = TypeDescriptor.GetProperties(obj.D520.GetType())[pi.Name].Attributes;
- DescriptionAttribute myAttribute = (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];
- string describle = myAttribute.Description;
- string proname = pi.Name;
- var abc = pi.GetValue(obj.D520, null);
- switch (proname)
- {
- default:
- break;
- }
- var values = new object[3];
- values[0] = describle;
- values[1] = proname;
- values[2] = abc;
- tb.Rows.Add(values);
- }
- foreach (PropertyInfo pi in obj.D537.GetType().GetProperties())
- {
- AttributeCollection attributes = TypeDescriptor.GetProperties(obj.D537.GetType())[pi.Name].Attributes;
- DescriptionAttribute myAttribute = (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];
- string describle = myAttribute.Description;
- string proname = "DB537" + pi.Name;
- var abc = pi.GetValue(obj.D537, null);
- switch (pi.Name)
- {
- case "TASKNUM":
- describle = "任务号";
- break;
- }
- var values = new object[3];
- values[0] = describle;
- values[1] = proname;
- values[2] = abc;
- tb.Rows.Add(values);
- }
- //属性遍历
- return tb;
- }
- }
- }
|