155 lines
4.1 KiB
C#
155 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using GetComObj;
|
|
using MSXML2;
|
|
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);
|
|
}
|
|
|
|
if (prop.PropertyType == typeof(DateTime))
|
|
{
|
|
prop.SetValue(obj, DateTime.Parse($"{value}"));
|
|
}
|
|
|
|
if (prop.PropertyType == typeof(byte))
|
|
{
|
|
prop.SetValue(obj, byte.Parse($"{value}"));
|
|
}
|
|
|
|
if (prop.PropertyType == typeof(int))
|
|
{
|
|
prop.SetValue(obj, int.Parse($"{value}"));
|
|
}
|
|
|
|
if (prop.PropertyType == typeof(long))
|
|
{
|
|
prop.SetValue(obj, long.Parse($"{value}"));
|
|
}
|
|
|
|
if (prop.PropertyType == typeof(decimal))
|
|
{
|
|
prop.SetValue(obj, decimal.Parse($"{value}"));
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|