using System; using System.Reflection; using Apache.NMS; using Microsoft.VisualBasic; namespace tcEAI_C { /// /// 20211027 13871,共用功能 /// public class clsCom { /// /// 20211027 13871,Reflection方法 /// /// DLL Name /// Class Name /// Function Name /// /// /// public static bool DetermineCustomized_Constructor(string strComponent, string strClassName, string strMethodName, ref MethodInfo mi, ref object obj) { bool DetermineCustomized_ConstructorRet = default(bool); bool blnResult = false; DetermineCustomized_ConstructorRet = false; try { blnResult = DetermineMethod_Constructor(strComponent, strClassName, strMethodName, ref mi, ref obj); } catch (Exception ex) { } finally { DetermineCustomized_ConstructorRet = blnResult; } return DetermineCustomized_ConstructorRet; } /// /// 20211027 13871,Reflection找出Method /// public static bool DetermineMethod_Constructor(string strComponent, string strClassName, string strMethodName, ref MethodInfo mi, ref object obj) { string strAppBase = AppDomain.CurrentDomain.BaseDirectory; string strFilePath = ""; Assembly mainAssembly; Type t; Type[] argumentTypes; ConstructorInfo ctor; MethodInfo[] aryMethos; try { strFilePath = strAppBase.TrimEnd('\\') + @"\bin\" + strComponent; mainAssembly = Assembly.LoadFrom(strFilePath); t = mainAssembly.GetType(mainAssembly.GetName().Name + "." + strClassName, true, true); // 20211211 13871,支援Module if (t.GetConstructors().Length > 0) { argumentTypes = Type.EmptyTypes; ctor = t.GetConstructor(argumentTypes); obj = ctor.Invoke(new object[] { }); } strMethodName = Strings.Replace(strMethodName, ".", "_"); aryMethos = t.GetMethods(); foreach (MethodInfo item in aryMethos) { if ((strMethodName.ToUpper() ?? "") == (item.Name.ToUpper() ?? "")) { mi = item; return true; } } if (mi == null) { return false; } else { return true; } } catch (Exception ex) { return false; } } /// /// 20211027 13871,取得Method的參數預設值陣列 /// /// DLL Name,可不傳副檔名,會自動補.dll /// Class Name /// Method Name /// public static object[] funGetMethodParameters(string strComponent, string strClassName, string strMethodName) { object[] funGetMethodParametersRet = default(object[]); try { var mi = default(MethodInfo); var obj = default(object); if (!strComponent.ToUpper().Contains(".DLL")) { strComponent = strComponent + ".dll"; } if (!DetermineCustomized_Constructor(strComponent, strClassName, strMethodName, ref mi, ref obj)) { throw new iMESException.MESException("0000-200003", strComponent + "." + strClassName + "," + strMethodName + " [%METHOD%] [%NOT FOUND%]"); } int iParameterLength = mi.GetParameters().Length; var aryParameter = new object[iParameterLength]; for (int i = 0, loopTo = mi.GetParameters().Length - 1; i <= loopTo; i++) { try { aryParameter[i] = mi.GetParameters()[i].DefaultValue; } catch (Exception) { if (mi.GetParameters()[i].ParameterType == typeof(DateTime)) aryParameter[i] = default(DateTime); else throw; } } funGetMethodParametersRet = aryParameter; } catch (Exception ex) { throw; } return funGetMethodParametersRet; } /// /// 20211027 13871,執行指定DLL、Class、Method,無法執行多載Method /// /// DLL Name,可不傳副檔名,會自動補.dll /// Class Name /// Method Name /// public static object funExecuteMethod(string strComponent, string strClassName, string strMethodName, ref object[] aryParameters) { object funExecuteMethodRet = default(object); try { var mi = default(MethodInfo); var obj = default(object); if (!strComponent.ToUpper().Contains(".DLL")) { strComponent = strComponent + ".dll"; } if (!DetermineCustomized_Constructor(strComponent, strClassName, strMethodName, ref mi, ref obj)) { throw new iMESException.MESException("0000-200003", strComponent + "." + strClassName + "," + strMethodName + " [%METHOD%] [%NOT FOUND%]"); } funExecuteMethodRet = mi.Invoke(obj, aryParameters); } catch (Exception ex) { ex = funGetRealException(ex); throw ex; } return funExecuteMethodRet; } // 20211208 13871,從clsEAP移植 public static Exception funGetRealException(Exception ex) { Exception funGetRealExceptionRet = default(Exception); try { funGetRealExceptionRet = ex; if (ex is TargetInvocationException && ex.InnerException != null) { funGetRealExceptionRet = funGetRealException(ex.InnerException); } } catch (Exception ex2) { throw; } return funGetRealExceptionRet; } #region --- Combine Input Xml --- public static string CombineXMLIdentity(ref string TransactionID, ref string ModuleID, ref string FunctionID, ref string ComputerName, ref string CurUserNo, ref string SendTime) { string CombineXMLIdentityRet = default(string); CombineXMLIdentityRet = "" + TransactionID + "" + "" + ModuleID + "" + "" + FunctionID + "" + "" + ComputerName + "" + "" + CurUserNo + "" + "" + SendTime + ""; return CombineXMLIdentityRet; } public static string CombineXMLParameter(ref string value_name, ref string name, ref string type, ref string value, ref string desc) { string CombineXMLParameterRet = default(string); CombineXMLParameterRet = "<" + value_name.ToLower() + ">" + "" + name + "" + "" + type + "" + "" + value + "" + "" + desc + "" + ""; return CombineXMLParameterRet; } public static string CombineXMLParameter(ref string name, ref string type, ref string value, ref string desc) { string CombineXMLParameterRet = default(string); CombineXMLParameterRet = "<" + name.ToLower() + ">" + "" + name + "" + "" + type + "" + "" + value + "" + "" + desc + "" + ""; return CombineXMLParameterRet; } public static string CombineXMLValue(ref string TagName, ref string Value) { string CombineXMLValueRet = default(string); CombineXMLValueRet = "<" + TagName + ">" + Value + ""; return CombineXMLValueRet; } public static string CombineXMLValueTag(ref string Value) { string CombineXMLValueTagRet = default(string); CombineXMLValueTagRet = "" + Value + ""; return CombineXMLValueTagRet; } public static string CombineXMLRequest(ref string strIdentity, ref string strParameter) { string CombineXMLRequestRet = default(string); CombineXMLRequestRet = "" + "" + strIdentity + ""; if (!string.IsNullOrEmpty(strParameter)) { CombineXMLRequestRet = CombineXMLRequestRet + "" + strParameter + ""; } CombineXMLRequestRet = CombineXMLRequestRet + ""; return CombineXMLRequestRet; } public static string CombineXMLParameterMultiValue(ref string value_name, ref string name, ref string type, ref string value, ref string desc) { string CombineXMLParameterMultiValueRet = default(string); // Value不用加上Tag CombineXMLParameterMultiValueRet = "<" + value_name.ToLower() + ">" + "" + name + "" + "" + type + "" + value + "" + desc + "" + ""; return CombineXMLParameterMultiValueRet; } public static string CombineXMLAdditional(ref string strAdditional) { string CombineXMLAdditionalRet = default(string); CombineXMLAdditionalRet = "" + strAdditional; CombineXMLAdditionalRet = CombineXMLAdditionalRet + ""; return CombineXMLAdditionalRet; } public static string CombineAddXML_Add(ref string name, ref string type, ref string value) { string CombineAddXML_AddRet = default(string); CombineAddXML_AddRet = "" + "" + name + "" + "" + type + "" + "" + value + "" + ""; return CombineAddXML_AddRet; } public static string CombineAddXML_Edit(ref string name, ref string type, ref string value) { string CombineAddXML_EditRet = default(string); CombineAddXML_EditRet = "" + "" + name + "" + "" + type + "" + "" + value + "" + ""; return CombineAddXML_EditRet; } public static string CombineAddXML_Field(ref string name) { string CombineAddXML_FieldRet = default(string); CombineAddXML_FieldRet = "" + "" + name + "" + ""; return CombineAddXML_FieldRet; } public static string CombineAddXML_Condition(string condition) { string CombineAddXML_ConditionRet = default(string); // 2007/03/22,sammi.呼叫CombineAddXML_Condition時,不可加CInput. // 因CInput會將單引號轉成二個單引號,若Condition為下述範例時,會Error. // strAdditional = CombineXMLAdditional(CombineAddXML_Condition("ModuleType in ('REWORK','SLR')")) // strParameter += strAdditional CombineAddXML_ConditionRet = "" + condition + ""; return CombineAddXML_ConditionRet; } public static string CInput(string strInput) { string CInputRet = default(string); // 將傳入值內的單引號轉換為可存入資料庫的格式 // 將傳入值內的 &, >, < 三個特殊字元轉換為XmlDocument可解譯之代替符號 // 傳入值: strInput包含特殊字元的字串 // 傳回值: 將特殊字元變更為代替符號的字串 // 轉換 ' 為 '' (單引號轉為兩個單引號) CInputRet = Strings.Replace(strInput, "'", "''"); // 轉換 & 為 & CInputRet = Strings.Replace(CInputRet, "&", "&"); // CInput = Replace(CInput, """", "''") 'AddFlow的Xml字串不可將雙引號轉為兩個單引號,XMLToFlow會Error // 轉換 > 為 > CInputRet = Strings.Replace(CInputRet, ">", ">"); // 轉換 < 為 < CInputRet = Strings.Replace(CInputRet, "<", "<"); return CInputRet; } #endregion } }