Exemple #1
0
        /// <summary>
        /// Construct a <see cref="ResourceInfo"/> object.
        /// </summary>
        /// <param name="type"></param>
        internal ResourceInfo(Type type)
        {
            m_ResourceType = type;

            // Determine Resource Manager to use
            ErrorStringResourceAttribute attr = (ErrorStringResourceAttribute)(type.GetCustomAttributes(typeof(ErrorStringResourceAttribute), true).FirstOrDefault())
                                                ?? (ErrorStringResourceAttribute)(m_ResourceType.Assembly.GetCustomAttributes(typeof(ErrorStringResourceAttribute), true).FirstOrDefault())
                                                ?? new ErrorStringResourceAttribute("ErrorStrings", type);

            if (attr.IsRedirect)
            {
                ResourceManager = ResourceManagerCache.ResourceInfoForType(attr.Redirect).ResourceManager;
            }
            else
            {
                ResourceManager = new ResourceManager(attr.Path, type.Assembly);
            }

            ErrorExceptionAttribute exceptionAttr = (ErrorExceptionAttribute)(type.GetCustomAttributes(typeof(ErrorExceptionAttribute), true).FirstOrDefault());

            DefaultExceptionConstructor =
                (exceptionAttr == null ? typeof(ApplicationException) : exceptionAttr.ExceptionType)
                .GetConstructor(new Type[] { typeof(string) });
            if (DefaultExceptionConstructor == null)
            {
                throw ErrorCode.ExceptionDoesNotContainProperConstructor_String.Exception(exceptionAttr.ExceptionType);
            }
        }
Exemple #2
0
        /// <summary>
        /// Create an exception for an error code.
        /// </summary>
        /// <param name="enumeration">The enumeration field specifying the error to create.</param>
        /// <param name="information">Formatting information specific to the error string.</param>
        /// <returns></returns>
        public static Exception Exception(this System.Enum enumeration, params object[] information)
        {
            Type            enumerationType = enumeration.GetType();
            ConstructorInfo constructor;

            object[] attrs = enumerationType.GetField(enumeration.ToString()).GetCustomAttributes(typeof(ErrorExceptionAttribute), true);
            if (attrs == null || attrs.Length == 0)
            {
                constructor = ResourceManagerCache.ResourceInfoForType(enumerationType).DefaultExceptionConstructor;
            }
            else
            {
                ErrorExceptionAttribute attribute = (ErrorExceptionAttribute)(attrs[0]);
                constructor = attribute.ExceptionType.GetConstructor(new Type[] { typeof(string) });
                if (constructor == null)
                {
                    throw ErrorCode.ExceptionDoesNotContainProperConstructor_String.Exception(attribute.ExceptionType);
                }
            }
            return((Exception)constructor.Invoke(new object[] { enumeration.FormatErrorString(information) }));
        }