Beispiel #1
0
        public Exception Resolve(string exceptionKey, object[] constructorArgs, Exception innerException, params object[] messageArgs)
        {
            ArgumentHelperExtensions.AssertNotNull <string>(exceptionKey, "exceptionKey");
            XElement xelement = Enumerable.FirstOrDefault <XElement>(Enumerable.Select(Enumerable.Where(Enumerable.SelectMany(ExceptionHelper.GetExceptionInfo(this._forType.Assembly).Element((XName)"exceptionHelper").Elements((XName)"exceptionGroup"), (Func <XElement, IEnumerable <XElement> >)(exceptionGroup => exceptionGroup.Elements((XName)"exception")), (exceptionGroup, exception) => new
            {
                exceptionGroup = exceptionGroup,
                exception      = exception
            }), param0 =>
            {
                if (string.Equals(param0.exceptionGroup.Attribute((XName)"type").Value, this._forType.FullName, StringComparison.Ordinal))
                {
                    return(string.Equals(param0.exception.Attribute((XName)"key").Value, exceptionKey, StringComparison.Ordinal));
                }
                else
                {
                    return(false);
                }
            }), param0 => param0.exception));

            if (xelement == null)
            {
                throw new InvalidOperationException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "The exception details for key '{0}' could not be found at /exceptionHelper/exceptionGroup[@type'{1}']/exception[@key='{2}'].", (object)exceptionKey, (object)this._forType, (object)exceptionKey));
            }
            else
            {
                XAttribute xattribute = xelement.Attribute((XName)"type");
                if (xattribute == null)
                {
                    throw new InvalidOperationException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "The '{0}' attribute could not be found for exception with key '{1}'", new object[2]
                    {
                        (object)"type",
                        (object)exceptionKey
                    }));
                }
                else
                {
                    Type type = Type.GetType(xattribute.Value);
                    if (type == null)
                    {
                        throw new InvalidOperationException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "Type '{0}' could not be loaded for exception with key '{1}'", new object[2]
                        {
                            (object)xattribute.Value,
                            (object)exceptionKey
                        }));
                    }
                    else if (!typeof(Exception).IsAssignableFrom(type))
                    {
                        throw new InvalidOperationException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "Type '{0}' for exception with key '{1}' does not inherit from '{2}'", (object)type.FullName, (object)exceptionKey, (object)typeof(Exception).FullName));
                    }
                    else
                    {
                        string format = xelement.Value.Trim();
                        if (messageArgs != null && messageArgs.Length > 0)
                        {
                            format = string.Format((IFormatProvider)CultureInfo.InvariantCulture, format, messageArgs);
                        }
                        List <object> list = new List <object>();
                        list.Add((object)format);
                        if (constructorArgs != null)
                        {
                            list.AddRange((IEnumerable <object>)constructorArgs);
                        }
                        if (innerException != null)
                        {
                            list.Add((object)innerException);
                        }
                        object[]        args            = list.ToArray();
                        BindingFlags    bindingAttr     = BindingFlags.Instance | BindingFlags.Public;
                        ConstructorInfo constructorInfo = (ConstructorInfo)null;
                        try
                        {
                            object state;
                            constructorInfo = (ConstructorInfo)Type.DefaultBinder.BindToMethod(bindingAttr, (MethodBase[])type.GetConstructors(bindingAttr), ref args, (ParameterModifier[])null, (CultureInfo)null, (string[])null, out state);
                        }
                        catch (MissingMethodException ex)
                        {
                        }
                        if (constructorInfo != null)
                        {
                            return((Exception)constructorInfo.Invoke(args));
                        }
                        throw new InvalidOperationException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "An appropriate constructor could not be found for exception type '{0}, for exception with key '{1}'", new object[2]
                        {
                            (object)type.FullName,
                            (object)exceptionKey
                        }));
                    }
                }
            }
        }
Beispiel #2
0
 public ExceptionHelper(Type forType)
 {
     ArgumentHelperExtensions.AssertNotNull <Type>(forType, "forType");
     this._forType = forType;
 }