Files
U8API/UFIDA.U8.Framework.Api.ControlObject/Common/CommonHelper.cs
T
2024-12-10 09:03:59 +08:00

319 lines
9.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using GetComObj;
using MSXML2;
using Newtonsoft.Json.Linq;
using UFIDA.U8.Framework.Api.ControlObject.Json;
namespace UFIDA.U8.Framework.Api.ControlObject.Common
{
public static class CommonHelper
{
public static void SetPropValue(this PropertyInfo prop, object obj, object value)
{
if (prop.PropertyType == typeof(string))
{
prop.SetValue(obj, value);
return;
}
if (prop.PropertyType == typeof(DateTime))
{
prop.SetValue(obj, DateTime.Parse($"{value}"));
return;
}
if (prop.PropertyType == typeof(byte))
{
prop.SetValue(obj, byte.Parse($"{value}"));
return;
}
if (prop.PropertyType == typeof(int))
{
prop.SetValue(obj, int.Parse($"{value}"));
return;
}
if (prop.PropertyType == typeof(long))
{
prop.SetValue(obj, long.Parse($"{value}"));
return;
}
if (prop.PropertyType == typeof(decimal))
{
prop.SetValue(obj, decimal.Parse($"{value}"));
return;
}
if (prop.PropertyType == typeof(double))
{
prop.SetValue(obj, double.Parse($"{value}"));
return;
}
if (prop.PropertyType == typeof(float))
{
prop.SetValue(obj, float.Parse($"{value}"));
return;
}
if (prop.PropertyType == typeof(short))
{
prop.SetValue(obj, short.Parse($"{value}"));
return;
}
}
//public static void SetPropValue(this PropertyInfo prop, VoucherData obj, object value)
//{
// if (prop.PropertyType == typeof(string))
// {
// prop.SetValue(obj, value);
// return;
// }
// if (prop.PropertyType == typeof(DateTime))
// {
// prop.SetValue(obj, DateTime.Parse($"{value}"));
// return;
// }
// if (prop.PropertyType == typeof(byte))
// {
// prop.SetValue(obj, byte.Parse($"{value}"));
// return;
// }
// if (prop.PropertyType == typeof(int))
// {
// prop.SetValue(obj, int.Parse($"{value}"));
// return;
// }
// if (prop.PropertyType == typeof(long))
// {
// prop.SetValue(obj, long.Parse($"{value}"));
// return;
// }
// if (prop.PropertyType == typeof(decimal))
// {
// prop.SetValue(obj, decimal.Parse($"{value}"));
// return;
// }
// if (prop.PropertyType == typeof(double))
// {
// prop.SetValue(obj, double.Parse($"{value}"));
// return;
// }
// if (prop.PropertyType == typeof(float))
// {
// prop.SetValue(obj, float.Parse($"{value}"));
// return;
// }
// if (prop.PropertyType == typeof(short))
// {
// prop.SetValue(obj, short.Parse($"{value}"));
// return;
// }
// //if (prop.PropertyType == typeof(object))
// //{
// // switch (prop.Name)
// // {
// // case "cDefine1":
// // obj.set_cDefine1($"{value}");
// // break;
// // case "cDefine2":
// // obj.set_cDefine2($"{value}");
// // break;
// // case "cDefine3":
// // obj.set_cDefine3($"{value}");
// // break;
// // case "cDefine4":
// // obj.set_cDefine4(DateTime.Parse($"{value}"));
// // break;
// // case "cDefine5":
// // obj.set_cDefine5(int.Parse($"{value}"));
// // break;
// // case "cDefine6":
// // obj.set_cDefine6(DateTime.Parse($"{value}"));
// // break;
// // case "cDefine7":
// // obj.set_cDefine7(float.Parse($"{value}"));
// // break;
// // case "cDefine8":
// // obj.set_cDefine8($"{value}");
// // break;
// // case "cDefine9":
// // obj.set_cDefine9($"{value}");
// // break;
// // case "cDefine10":
// // obj.set_cDefine10($"{value}");
// // break;
// // case "cDefine11":
// // obj.set_cDefine11($"{value}");
// // break;
// // case "cDefine12":
// // obj.set_cDefine12($"{value}");
// // break;
// // case "cDefine13":
// // obj.set_cDefine13($"{value}");
// // break;
// // case "cDefine14":
// // obj.set_cDefine14($"{value}");
// // break;
// // case "cDefine15":
// // obj.set_cDefine15(int.Parse($"{value}"));
// // break;
// // case "cDefine16":
// // obj.set_cDefine16(float.Parse($"{value}"));
// // break;
// // default:
// // prop.SetValue(obj, $"{value}");
// // break;
// // }
// //}
//}
public static IXMLDOMDocument2 ToDocument2(this Dictionary<string,object> dictionary,IXMLDOMDocument2 domDocument2=null)
{
if (domDocument2 == null)
{
domDocument2 = new DOMDocument();
}
var root = domDocument2.ClearDocument();
var newRow = SetRowData(dictionary, domDocument2);
root.appendChild(newRow);
return domDocument2;
}
public static IXMLDOMNode SetRowData(this Dictionary<string, object> dictionary, IXMLDOMDocument2 domDocument2 = null,string bodyKey="body")
{
if (domDocument2 == null)
{
domDocument2 = new DOMDocument();
}
var newRow = domDocument2.createElement("z:row");
foreach (var key in dictionary.Keys)
{
if(key==bodyKey)
continue;
var attr = domDocument2.createAttribute(key);
attr.value = dictionary[key];
newRow.attributes.setNamedItem(attr);
}
return newRow;
}
public static IXMLDOMDocument2 ToDocument2(this List<Dictionary<string, object>> dictionaryList, IXMLDOMDocument2 domDocument2 = null)
{
if (domDocument2 == null)
{
domDocument2 = new DOMDocument();
}
var root = domDocument2.ClearDocument();
foreach (var dictionary in dictionaryList)
{
var newRow = SetRowData(dictionary, domDocument2);
root.appendChild(newRow);
}
return domDocument2;
}
public static IXMLDOMNode ClearDocument(this IXMLDOMDocument2 domDocument2)
{
var root = domDocument2.selectSingleNode("//rs:data");
if (root == null)
{
root = domDocument2.createElement("rs:data");
domDocument2.appendChild(root);
}
var rows = domDocument2.selectNodes("//z:row");
if (rows != null && rows.length > 0)
{
for (var i = 0; i < rows.length; i++)
{
var row = rows[i];
root.removeChild(row);
}
}
return root;
}
public static void ResetDictionary(this Dictionary<string, object> dictionary, string bodyKey = "body")
{
if (dictionary.ContainsKey(bodyKey))
{
var body= dictionary[bodyKey].ToJson().ToList<Dictionary<string,object>>();
dictionary[bodyKey] = body;
}
}
public static dynamic GetComObject(this string comName)
{
var comInstance = new GetComInstance();
var obj = comInstance.GetComObject(ref comName);
return obj;
}
public static string U8Encrypt(string input)
{
var rethash = "";
var hash = SHA1.Create();
var encoder = new ASCIIEncoding();
var combined = encoder.GetBytes(input);
hash.ComputeHash(combined);
rethash = Convert.ToBase64String(hash.Hash);
if (string.IsNullOrWhiteSpace(rethash))
return rethash;
return rethash + (char)3;
}
}
}