| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- 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<int, string> _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<int, string>();
- var consts = exceptionCodesStaticClass.GetType().GetFields(BindingFlags.Public | BindingFlags.Static);
- foreach (FieldInfo con in consts)
- {
- var attr = AttributeQuery<CodeMessageExceptionItemAttribute>.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();
- }
- }
- }
- }
- }
- }
|