---------- using System; using System.Collections.Generic; using System.Linq; using System.Text; // add using System.Collections; using ZLib.Core; using System.Reflection; using System.Diagnostics; // 備忘 // 子類別必須沿用的屬性(不加abstract或virtual) // 子類別可覆寫的屬性(需加abstract或virtual) // 子類別必須實作的屬性或方法 (abstract) // static成員 與 override, virtual, abstract互斥 namespace ZLib.Core.DAbstract { /// /// 京奇專案架構, 適用所有專案(含Service/Web/WebService). /// 模型以instance架構為主. 專案中應控制只建立一個Project Instance: static CCommon -> mProject(Instance of CProject). /// public abstract class AZProject { //private string msRun3 = "誰先執行?"; const string csExtraErrorMsg = "ExtErr"; // 變數範例 // 備忘: // 1 呼叫順序: (new 子類別instance) --> ((子類別static constructor()) --> (抽象類別static constructor()) --> (抽象類別instance constructor()) --> (子類別instance constructor()) // 2 ZAProject的介面需注意與ZIProject介面一致. private static DateTime gdLaunch; // static啟動時間 private static long glCount; // General Static No. private long mlCount; // General Instance No. private static Exception gException; protected static TraceSwitch mTraceSwitch; protected static Boolean mbDebug; // Runtime debug flag. // abstract static constructor不(怎麼)支援多載變化, 別再試了! static AZProject() { // 請了解並注意執行呼叫順序 // 呼叫順序: (new 子類別instance) --> ((子類別static constructor()) --> (抽象類別static constructor()) --> (抽象類別instance constructor()) --> (子類別instance constructor()) gdLaunch = DateTime.Now; ResetStaticNo(); } // abstract constructor不支援多載變化, 別再試了! public AZProject() { // 請了解並注意呼叫順序 // 呼叫順序: (new 子類別instance) --> ((子類別static constructor()) --> (抽象類別static constructor()) --> (抽象類別instance constructor()) --> (子類別instance constructor()) // 京奇專案架構應控制Application只建立一次Project Instance, 不對的話, 請注意是否需要調整設計! if (GetNextStaticNo() != 1) Trace.WriteLineIf(ZProject.TraceLevel2Warning, "京奇專案架構應控制Application只建立一次Project Instance, 不對的話, 請注意是否需要調整設計! Static No=" + StaticNo); ResetInstanceNo(); } // 屬性 public static DateTime LaunchTime { get { return gdLaunch; } } public static String LaunchID { get { return gdLaunch.ZToString_yyyyMMddHHmmssfff(); } } public static long StaticNo { get { return glCount; } } public static Assembly gExecutingAssembly { get { return ZExeProgram.ZGetExecutingAssembly(); } } public long InstanceNo { get { return mlCount; } } public string ReleaseVersion { get { return ReleaseTime.ZToString_Version(); } } protected string msRootDir; protected int miRuntimeEnvironment; public static bool DebugFlag { get { return mbDebug; } } public static bool TraceLevel1Error { get { return mTraceSwitch.TraceError; } } public static bool TraceLevel2Warning { get { return mTraceSwitch.TraceWarning; } } public static bool TraceLevel3Info { get { return mTraceSwitch.TraceInfo; } } public static bool TraceLevel4Verbose { get { return mTraceSwitch.TraceVerbose; } } // 可覆寫虛擬屬性. 抽象方法隱含是一個虛擬方法, 因此virtual子句為多餘. //public virtual string ProjectName { get; private set; } // 抽象屬性.需實作. (static成員 與 override, virtual, abstract互斥) public abstract DateTime ReleaseTime { get; } public abstract string CompanyID { get; } public abstract string SystemID { get; } public abstract string ProjectID { get; } public abstract string ProductID { get; } public abstract string CompanyName { get; } public abstract string SystemName { get; } public abstract string ProjectName { get; } public abstract string ProductName { get; } public abstract string CompanyShortName { get; } public static abstract string PathLog { get; } public static abstract string PathRoot { get; } public static abstract string PathData { get; } // 方法 public static void ResetStaticNo() { glCount = 0; } public static long GetNextStaticNo() { // not working...這個寫法不但沒用, 還會搞當系統, 怪! //glCount = (glCount == long.MaxValue) ? 0 : (glCount++); if (glCount == long.MaxValue) glCount = 0; else glCount++; return glCount; } public void ResetInstanceNo() { mlCount = 0; } public long GetNextInstanceNo() { // not working...這個寫法不但沒用, 還會搞當系統, 怪! 請以static no版本Debug此問題! //return (mlCount == long.MaxValue) ? 0 : mlCount++; if (mlCount == long.MaxValue) mlCount = 0; else mlCount++; return mlCount; } public static void ErrorClear() { gException = null; } public static bool IsError { get { return (gException == null ? false : true); } } //public static string ErrorMessage //{ // get { return (gException == null ? string.Empty : gException.Message); } //} public static void ErrorDataAdd(object key, object value) { if (gException == null) return; gException.Data.Add(key, value); } public static void ErrorDataAdd(string sKey, string sFormat, params object[] poa) { if (gException == null) return; gException.Data.Add(sKey, string.Format(sFormat, poa)); } public static void ErrorDataAddWhere(string sWhere) { ErrorDataAdd(csExtraErrorMsg, sWhere); } public static void ErrorDataAddWhere(string sFormat, params object[] poa) { ErrorDataAddWhere(string.Format(sFormat, poa)); } public static void ErrorSet(string sFormat, params object[] poa) { ErrorClear(); gException = new ApplicationException(string.Format(sFormat, poa)); } public static void ErrorSet(Exception ex, string sFormat, params object[] poa) { ErrorClear(); gException = ex; ErrorDataAddWhere(string.Format(sFormat, poa)); } public static void ErrorSet(string sError) { ErrorClear(); gException = new ApplicationException(sError); } public static void ErrorSet(Exception ex) { ErrorClear(); gException = ex; } public static void ErrorSet(Exception ex, string sWhere) { ErrorClear(); gException = ex; ErrorDataAddWhere(sWhere); } public static string GetErrorMessage() { if (!IsError) return null; StringBuilder sb = new StringBuilder(); sb.Length = 0; Exception ex = gException; while (ex != null) { sb.AppendLine(ex.Message); if (ex.Data.Contains(csExtraErrorMsg)) sb.AppendLine(ex.Data[csExtraErrorMsg].ToString()); ex = ex.InnerException; } return sb.ToString(); } public static string[] GetErrorMessages() { if (!IsError) return null; ArrayList a1 = new ArrayList(); Exception ex = gException; while (ex != null) { a1.Add(ex.Message); if (ex.Data.Contains(csExtraErrorMsg)) a1.Add(gException.Message); ex = ex.InnerException; } return a1.ZCopyToStringArray(); } //public static void WriteLineIfWarning(string sMsg) //{ // // 盡量直接呼叫Trace // Trace.WriteLineIf(TraceLevel2Warning, "京奇專案架構應控制Application只建立一次Project Instance, 不對的話, 請注意是否需要調整設計! Static No=" + StaticNo); //} // 可複寫虛擬方法. 抽象方法隱含就是一個虛擬方法, 因此virtual子句為多餘. //public virtual void InitAbstract(string sCompanyID, string sSystemID, string sProject, DateTime dRelease) //{ // ProjectID = sProject; //} // 抽象方法.需實作 public abstract bool LoadConfig(); public abstract bool SaveConfig(); } }