dc/Utilities/SECSUtil.cs
13118993771@163.com 562bd7a7b8 新版
2023-02-23 11:33:23 +08:00

177 lines
6.3 KiB
C#

// ********************************************************************************
// 文件名字: SECSUtil
// 文件描述: SECSUtil
// 开发人员: Michael
// 创建时间: 2019/11/8 0:08
//
// 更新历史:
// + 创建 SECSUtil.cs 文件. by Michael @2019/11/8 0:08
// ********************************************************************************
using Glorysoft.SECSwell;
using Newtonsoft.Json;
using System;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace Glorysoft.SECS.EQP.Utilities
{
public static class SECSUtil
{
public static SECSItem CreateSECSItem(string name, eSECS_FORMAT type, object value = null)
{
return new SECSItem(type, name)
{
Value = value
};
}
public static eSECS_FORMAT GetSECS_FORMAT(this object value)
{
if (value == null)
{
return eSECS_FORMAT.ASCII;
}
switch (value.GetType().Name)
{
case nameof(Byte): return eSECS_FORMAT.U1;
case nameof(SByte): return eSECS_FORMAT.I1;
case nameof(UInt16): return eSECS_FORMAT.U2;
case nameof(Int16): return eSECS_FORMAT.I2;
case nameof(UInt32): return eSECS_FORMAT.U4;
case nameof(Int32): return eSECS_FORMAT.I4;
case nameof(UInt64): return eSECS_FORMAT.U8;
case nameof(Int64): return eSECS_FORMAT.I8;
case nameof(Single): return eSECS_FORMAT.F4;
case nameof(Double): return eSECS_FORMAT.F8;
case nameof(String): return eSECS_FORMAT.ASCII;
case nameof(Boolean): return eSECS_FORMAT.BOOLEAN;
default: return eSECS_FORMAT.ASCII;
}
}
public static TObject To<TObject>(this object input)
{
if (input == null)
{
return default(TObject);
}
var targetType = typeof(TObject);
if (targetType.IsEnum)
{
return (TObject)Enum.Parse(targetType, input.ToString());
}
if (targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
if (input == null || input.ToString().Length == 0)
{
return default(TObject);
}
NullableConverter nullableConverter = new NullableConverter(targetType);
targetType = nullableConverter.UnderlyingType;
}
return (TObject)Convert.ChangeType(input, targetType);
}
public static TObject To<TObject>(this object input, TObject defaultValue = default(TObject))
{
try
{
return input.To<TObject>();
}
catch
{
return defaultValue;
}
}
#region Xml序列化
/// <summary>
/// 将对象序列化为Xml字符串.
/// </summary>
/// <typeparam name="TObject">对象的类型.</typeparam>
/// <param name="this">序列化的对象.</param>
/// <param name="encoding">字符编码.</param>
/// <returns>成功返回Xml字符串, 失败返回 null.</returns>
public static string SerializeXml<TObject>(this TObject @this, Encoding encoding = null)
{
XmlSerializer serializer = new XmlSerializer(@this.GetType());
using (MemoryStream memoryStream = new MemoryStream())
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
using (XmlWriter writer = XmlWriter.Create(memoryStream, new XmlWriterSettings
{
Indent = true,
Encoding = Encoding.UTF8,
OmitXmlDeclaration = true
}))
{
serializer.Serialize(writer, @this, ns);
}
memoryStream.Position = 0;
using (StreamReader reader = new StreamReader(memoryStream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
}
/// <summary>
/// 将Xml符串反序列化为对象.
/// </summary>
/// <typeparam name="TObject">对象的类型.</typeparam>
/// <param name="this">反序列化的Xml字符串.</param>
/// <param name="encoding">字符编码.</param>
/// <returns>成功返回 TObject 对象, 失败返回 null.</returns>
public static TObject DeserializeXml<TObject>(this string @this, Encoding encoding = null)
{
using (MemoryStream memoryStream = new MemoryStream((encoding == null ? Encoding.Default.GetBytes(@this) : encoding.GetBytes(@this))))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(TObject));
return (TObject)xmlSerializer.Deserialize(memoryStream);
}
}
#endregion Xml序列化
public static string ToJson<T>(this T input, bool indented = true)
{
return JsonConvert.SerializeObject(input, indented ? Newtonsoft.Json.Formatting.Indented : Newtonsoft.Json.Formatting.None);
}
public static T FromJson<T>(this string input)
{
return JsonConvert.DeserializeObject<T>(input);
}
/// <summary>
/// 对象转Get请求参数
/// </summary>
/// <returns></returns>
public static string ObjectToGetParam(this object obj)
{
StringBuilder strBui = new StringBuilder();
System.Reflection.PropertyInfo[] proArray = obj.GetType().GetProperties();
foreach (System.Reflection.PropertyInfo pro in proArray)
{
if (strBui.Length < 1)
{
strBui.Append("?");
}
else
{
strBui.Append("&");
}
strBui.Append(string.Format("{0}={1}", pro.Name, pro.GetValue(obj, null)));
}
return strBui.ToString();
}
}
}