Client/Desktop/KMBIM3.0/KMBIM3.0_23.08.16_수정최종/Cmd/Insulation_bak/DataManager.cs

148 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;
namespace KMBIM.Revit.Tools.LogicClass
{
//[System.Reflection.Obfuscation(Exclude = false, ApplyToMembers = true, Feature = "preset(Aggressive)")]
// [System.Reflection.Obfuscation(Exclude = false, Feature = "-rename(renXaml=false)")]
[System.Reflection.Obfuscation(Exclude = true)]
static class DataManager
{
/// <para>Specify whether to remove xml namespaces.</para>para>
/// <para>If your object has any XmlInclude attributes, then set this to false</para>
/// </param>
/// <returns>Serialized XML for specified System.Object</returns>
[System.Reflection.Obfuscation(Exclude = true)]
public static string XmlSerialize<T>(this T item, bool removeNamespaces = true)
{
object locker = new object();
XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
xmlns.Add(string.Empty, string.Empty);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
settings.Encoding = Encoding.UTF8;
lock (locker)
{
StringBuilder stringBuilder = new StringBuilder();
using (StringWriter stringWriter = new StringWriter(stringBuilder))
{
using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, settings))
{
if (removeNamespaces)
{
xmlSerializer.Serialize(xmlWriter, item, xmlns);
}
else { xmlSerializer.Serialize(xmlWriter, item); }
return stringBuilder.ToString();
}
}
}
}
[System.Reflection.Obfuscation(Exclude = true)]
public static TClass Deserialize<TClass>(string xml) where TClass : class, new()
{
var tClass = new TClass();
//불러오기 할 경우 필요없는 데이터 먼저 삭제 하자
// ((WorkMain)tClass).tblInternalStandard.m_table.RemoveAll();
xml = RemoveTypeTagFromXml(xml);
var xmlSerializer = new XmlSerializer(typeof(TClass));
using (TextReader textReader = new StringReader(xml))
{
tClass = (TClass)xmlSerializer.Deserialize(textReader);
}
return tClass;
}
[System.Reflection.Obfuscation(Exclude = true)]
public static string RemoveTypeTagFromXml(string xml)
{
if (!string.IsNullOrEmpty(xml) && xml.Contains("xsi:type"))
{
xml = Regex.Replace(xml, @"\s+xsi:type=""\w+""", "");
}
return xml;
}
#region Base64
public static string Base64Encode(string data)
{
try
{
byte[] encData_byte = new byte[data.Length];
encData_byte = System.Text.Encoding.UTF8.GetBytes(data);
string encodedData = Convert.ToBase64String(encData_byte);
return encodedData;
}
catch (Exception e)
{
throw new Exception("Error in Base64Encode: " + e.Message);
}
}
public static string Base64Decode(string data)
{
try
{
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(data);
int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
string result = new String(decoded_char);
return result;
}
catch (Exception e)
{
throw new Exception("Error in Base64Decode: " + e.Message);
}
}
#endregion
}
}