public static bool DataTableDeserialize(string fileName, out DataTable dt, bool isEncrypt = false) { bool result = false; dt = null; try { string filePath = LkCommonUtil.GetFilePath(Path.Combine(DefaultDirectory, fileName)); if (File.Exists(filePath)) { XmlSerializer serializer = new XmlSerializer(typeof(DataTable)); string data = File.ReadAllText(filePath); string temp = string.Empty; if (isEncrypt && LkEncryptDecrypt.Decrypt(data, out temp)) { data = temp; } StringReader dataSr = new StringReader(data); XmlReader reader = XmlReader.Create(dataSr); dt = serializer.Deserialize(reader) as DataTable; result = true; } } catch (Exception e) { throw e; } return(result); }
/// <summary> /// 取得Dll中某個Class的Instance /// </summary> /// <param name="dllPath">DLL 的路徑</param> /// <param name="className">Class name(ClassLibrary1.Class1)</param> /// <param name="output">輸出ReflectInstance,之後可以用該實體去呼叫Method</param> /// <returns></returns> public static bool GetDllClassInstance(string dllPath, string className, out LkReflectModel output) { output = new LkReflectModel(); bool result = false; try { //output.AppDomainObj = AppDomain.CreateDomain("some"); //Assembly assembly = null; //output.AppDomainObj.DoCallBack(() => //{ // assembly = Assembly.LoadFrom(dllPath); //}); //output.AssemblyDll = assembly; //TODO 不會被lock的方法 //byte[] dllBytes = System.IO.File.ReadAllBytes(dllPath); //output.AssemblyDll = Assembly.Load(dllBytes); //TODO 省memory output.Assembly = Assembly.LoadFile(LkCommonUtil.GetFilePath(dllPath.CheckExtansion("dll"))); output.Class = output.Assembly.GetType(className); output.ClassInstance = Activator.CreateInstance(output.Class); result = true; } catch (Exception e) { throw e; } return(result); }
/// <summary> /// 執行Method /// </summary> /// <param name="dllPath">DLL 的路徑></param> /// <param name="className">Class name</param> /// <param name="methodName">Method name</param> /// <param name="input">傳入的參數</param> /// <param name="output">回傳結果</param> /// <returns></returns> public static bool ExecuteMethod(string dllPath, string className, string methodName, object input, out object output) { bool result = false; output = null; try { Assembly assembly = Assembly.LoadFile(LkCommonUtil.GetFilePath(dllPath.CheckExtansion("dll"))); Type type = assembly.GetType(className); object instance = Activator.CreateInstance(type); MethodInfo methodInfo = type.GetMethod(methodName); if (input == null) { output = methodInfo.Invoke(instance, null); } else { output = methodInfo.Invoke(instance, new object[] { input }); } result = true; } catch (Exception e) { throw e; } return(result); }
public static string GetStringValue(string elementName, XElement element) { string result = string.Empty; try { result = LkCommonUtil.TrimText(element.Element(elementName).Value); } catch (Exception e) { throw e; } return(result); }
public static bool DataTableSerialize(string fileName, DataTable dt, bool isEncrypt = false, int backupSec = 0) { bool result = false; try { if (string.IsNullOrEmpty(dt.TableName)) { dt.TableName = fileName; } DateTime fileUpdateTime = DateTime.MinValue; string filePath = LkCommonUtil.GetFilePath(Path.Combine(DefaultDirectory, fileName)); string dirName = new FileInfo(filePath).DirectoryName; if (!Directory.Exists(dirName)) { Directory.CreateDirectory(dirName); } if (File.Exists(filePath)) { fileUpdateTime = File.GetLastWriteTime(filePath); } if (DateTime.Now.Subtract(fileUpdateTime).TotalSeconds > backupSec) { StringBuilder data = new StringBuilder(); XmlWriter writer = XmlWriter.Create(data); XmlSerializer serializer = new XmlSerializer(typeof(DataTable)); serializer.Serialize(writer, dt); if (isEncrypt) { string temp = string.Empty; if (LkEncryptDecrypt.Encrypt(data.ToString(), out temp)) { File.WriteAllText(filePath, temp); result = true; } } else { File.WriteAllText(filePath, data.ToString()); result = true; } } } catch (Exception e) { throw e; } return(result); }
public static bool ObjectSerialize(string fileName, object obj, bool isEncrypt = false, int backupSec = 0) { bool result = false; try { DateTime fileUpdateTime = DateTime.MinValue; string filePath = LkCommonUtil.GetFilePath(Path.Combine(DefaultDirectory, fileName)); if (File.Exists(filePath)) { fileUpdateTime = File.GetLastWriteTime(filePath); } if (DateTime.Now.Subtract(fileUpdateTime).TotalSeconds > backupSec) { Polenter.Serialization.SharpSerializer serializer = new Polenter.Serialization.SharpSerializer(); serializer.Serialize(obj, filePath); if (isEncrypt) { string temp = string.Empty; if (LkEncryptDecrypt.Encrypt(File.ReadAllText(filePath), out temp)) { File.WriteAllText(filePath, temp); result = true; } } else { result = true; } } } catch (Exception e) { throw e; } return(result); }
public static bool ObjectDeserialize(string fileName, out object obj, bool isEncrypt = false) { bool result = false; obj = null; try { string filePath = LkCommonUtil.GetFilePath(Path.Combine(DefaultDirectory, fileName)); if (File.Exists(filePath)) { Polenter.Serialization.SharpSerializer serializer = new Polenter.Serialization.SharpSerializer(); if (isEncrypt) { string temp = string.Empty; if (LkEncryptDecrypt.Decrypt(File.ReadAllText(filePath), out temp)) { File.WriteAllText(filePath + ".temp", temp); obj = serializer.Deserialize(filePath + ".temp"); File.Delete(filePath + ".temp"); result = true; } } else { obj = serializer.Deserialize(filePath); result = true; } } } catch (Exception e) { throw e; } return(result); }