using System; using System.Collections.Generic; using System.Reflection; using Quadarax.Foundation.Core.Attributes; using Quadarax.Foundation.Core.Reflection; namespace Quadarax.Foundation.Core.Exceptions { [Serializable] public class CodeException : Exception { private static IDictionary _codeMessageCache; public CodeException(int code, string message, Exception innerException) : base(message, innerException) { this.SetCode(code); } public CodeException(int code, string message) : base(message) { this.SetCode(code); } public CodeException(int code) : base() { this.SetCode(code); } public static void InitMessageCache(object exceptionCodesStaticClass) { if (_codeMessageCache == null) _codeMessageCache = new Dictionary(); var consts = exceptionCodesStaticClass.GetType().GetFields(BindingFlags.Public | BindingFlags.Static); foreach (FieldInfo con in consts) { var attr = AttributeQuery.Get(con); if (attr != null) { if (!_codeMessageCache.ContainsKey(attr.Code)) { _codeMessageCache.Add(attr.Code, con.GetValue(exceptionCodesStaticClass)?.ToString()); } else { _codeMessageCache[attr.Code] = con.GetValue(exceptionCodesStaticClass)?.ToString(); } } } } } }