Ejemplo n.º 1
0
        public static void TargetException()
        {
            TargetException ex = new TargetException();

            Assert.NotEmpty(ex.Message);
            Assert.Null(ex.InnerException);
            Assert.Equal(COR_E_TARGET, ex.HResult);
        }
Ejemplo n.º 2
0
 public static PropertyAccessException GetTargetException(
     MemberInfo member,
     TargetException e)
 {
     var declaring = member.DeclaringType;
     var message = $"Failed to invoke method {member.Name} on class {declaring.CleanName()}: {e.InnerException.Message}";
     throw new PropertyAccessException(message, e);
 }
Ejemplo n.º 3
0
        public static void TargetException_Message()
        {
            string          message = "message";
            TargetException ex      = new TargetException(message);

            Assert.Equal(message, ex.Message);
            Assert.Null(ex.InnerException);
            Assert.Equal(COR_E_TARGET, ex.HResult);
        }
 public void HandleException(
     object targetObject,
     MethodInfo fastMethod,
     TargetException ex,
     object[] parameters)
 {
     log.Error("Exception encountered: " + ex.InnerException?.Message, ex.InnerException);
     HandleExceptionCommon(targetObject, fastMethod, ex.InnerException, parameters);
 }
Ejemplo n.º 5
0
        public static void TargetException_Message_InnerException()
        {
            string          message = "message";
            Exception       inner   = new Exception();
            TargetException ex      = new TargetException(message, inner);

            Assert.Equal(message, ex.Message);
            Assert.Same(inner, ex.InnerException);
            Assert.Equal(COR_E_TARGET, ex.HResult);
        }
Ejemplo n.º 6
0
        public static PropertyAccessException GetTargetException(MethodInfo method, TargetException e)
        {
            Type   declaring = method.DeclaringType;
            String eMessage  = e.InnerException != null ? e.InnerException.Message : e.Message;
            String message   = "Failed to invoke method " + method.Name + " on class " +
                               TypeHelper.GetTypeNameFullyQualPretty(declaring) + ": " +
                               eMessage;

            throw new PropertyAccessException(message, e);
        }
Ejemplo n.º 7
0
        public static void TargetException()
        {
            TargetException ex = new TargetException();

            Assert.NotEmpty(ex.Message);
            Assert.Null(ex.InnerException);

            TargetException caught = Assert.Throws<TargetException>(() => ThrowGivenException(ex));
            Assert.Same(ex, caught);
        }
Ejemplo n.º 8
0
        public static void TargetException()
        {
            TargetException ex = new TargetException();

            Assert.NotEmpty(ex.Message);
            Assert.Null(ex.InnerException);

            TargetException caught = Assert.Throws <TargetException>(() => ThrowGivenException(ex));

            Assert.Same(ex, caught);
        }
Ejemplo n.º 9
0
        public static void TargetException_Message()
        {
            string message = "message";
            TargetException ex = new TargetException(message);

            Assert.Equal(message, ex.Message);
            Assert.Null(ex.InnerException);

            TargetException caught = Assert.Throws<TargetException>(() => ThrowGivenException(ex));
            Assert.Same(ex, caught);
        }
Ejemplo n.º 10
0
        public static void TargetException_Message()
        {
            string          message = "message";
            TargetException ex      = new TargetException(message);

            Assert.Equal(message, ex.Message);
            Assert.Null(ex.InnerException);

            TargetException caught = Assert.Throws <TargetException>(() => ThrowGivenException(ex));

            Assert.Same(ex, caught);
        }
Ejemplo n.º 11
0
        public static void TargetException()
        {
            Exception ex = new TargetException();

            Assert.NotNull(ex);
            Assert.Equal(ex.GetType(), typeof(TargetException));

            string s = "My exception";

            ex = new TargetException();
            Assert.NotNull(ex);
            Assert.Equal(ex.GetType(), typeof(TargetException));
            Assert.Equal(s, ex.Message);

            s = "My exception";
            Exception innerException = new Exception();

            ex = new TargetException(s, innerException);
            Assert.NotNull(ex);
            Assert.Equal(ex.GetType(), typeof(TargetException));
            Assert.Equal(innerException, ex.InnerException);
            Assert.Equal(s, ex.Message);

            // Throw the exception from a method.
            try
            {
                ThrowTargetException(s, innerException);
                Assert.True(false);
            }
            catch (TargetException tex)
            {
                Assert.Equal(innerException, tex.InnerException);
                Assert.Equal(s, tex.Message);
            }
            catch (Exception)
            {
                Assert.True(false);
            }
        }
Ejemplo n.º 12
0
 public void Error(TargetException ex)
 {
     Error (ex.Message);
 }
Ejemplo n.º 13
0
 public void Error(TargetException ex)
 {
     Error(ex.Message);
 }
Ejemplo n.º 14
0
        private static object CallMethodWithByRefParametersOrThrow(Type type, object target, string methodName, object[] args, TargetException te)
        {
            MethodInfo[] mis = type.GetMethods();

            foreach (var mi in mis)
            {
                var parameters = mi.GetParameters();

                if ((!mi.Name.Equals(methodName)) || (parameters.Length != args.Length))
                {
                    continue;
                }
                var         allMatch        = true;
                IList <int> byRefArgIndices = new List <int>();

                for (var i = 0; i < parameters.Length; i++)
                {
                    var rawParameterType = parameters[i].ParameterType;
                    var parameterType    = rawParameterType.IsByRef ? rawParameterType.GetElementType() : rawParameterType;

                    if (!(parameterType.IsInstanceOfType(args[i])))
                    {
                        allMatch = false;
                        break;
                    }
                    if (rawParameterType.IsByRef)
                    {
                        byRefArgIndices.Add(i);
                    }
                }

                if (!allMatch)
                {
                    continue;
                }
                {
                    var invocationResult = mi.Invoke(target, args);

                    if (byRefArgIndices.Count == 0)
                    {
                        return(invocationResult);
                    }
                    var result = new object[1 + byRefArgIndices.Count];
                    result[0] = invocationResult;

                    for (var i = 0; i < byRefArgIndices.Count; i++)
                    {
                        result[1 + i] = args[byRefArgIndices[i]];
                    }

                    return(result);
                }
            }

            throw te;
        }
Ejemplo n.º 15
0
        private static object CallMethodWithByRefParametersOrThrow(Type type, object target, string methodName, object[] args, TargetException te)
        {
            MethodInfo[] mis = type.GetMethods();

            foreach (MethodInfo mi in mis)
            {
                ParameterInfo[] parameters = mi.GetParameters();

                if ((mi.Name.Equals(methodName)) && (parameters.Length == args.Length))
                {
                    bool        allMatch        = true;
                    IList <int> byRefArgIndices = new List <int>();

                    for (int i = 0; i < parameters.Length; i++)
                    {
                        Type rawParameterType = parameters[i].ParameterType;
                        Type parameterType    = rawParameterType.IsByRef?rawParameterType.GetElementType():rawParameterType;

                        if (!(parameterType.IsAssignableFrom(args[i].GetType())))
                        {
                            allMatch = false;
                            break;
                        }
                        else if (rawParameterType.IsByRef)
                        {
                            byRefArgIndices.Add(i);
                        }
                    }

                    if (allMatch)
                    {
                        object invocationResult = mi.Invoke(target, args);

                        if (byRefArgIndices.Count == 0)
                        {
                            return(invocationResult);
                        }
                        else
                        {
                            object[] result = new object[1 + byRefArgIndices.Count];
                            result[0] = invocationResult;

                            for (int i = 0; i < byRefArgIndices.Count; i++)
                            {
                                result[1 + i] = args[byRefArgIndices[i]];
                            }

                            return(result);
                        }
                    }
                }
            }

            throw te;
        }
Ejemplo n.º 16
0
        private bool TryConvertValue(PSAdaptedProperty property, object valueObject, Type attrType, Type valType, out object newValue, out Exception conversionException)
        {
            bool flag;

            newValue            = null;
            conversionException = null;
            if (!attrType.IsEnum)
            {
                if (!(attrType == typeof(string)) || !valType.IsPrimitive && !(valueObject is Guid) && valueObject as ADObject == null)
                {
                    Type[] typeArray = new Type[1];
                    typeArray[0] = valType;
                    Type[]   typeArray1 = typeArray;
                    object[] objArray   = new object[1];
                    objArray[0] = valueObject;
                    object[]        objArray1   = objArray;
                    ConstructorInfo constructor = attrType.GetConstructor(typeArray1);
                    if (constructor != null)
                    {
                        try
                        {
                            newValue = constructor.Invoke(objArray1);
                            flag     = true;
                            return(flag);
                        }
                        catch (TargetInvocationException targetInvocationException1)
                        {
                            TargetInvocationException targetInvocationException = targetInvocationException1;
                            conversionException = targetInvocationException.InnerException;
                        }
                        catch (TargetException targetException1)
                        {
                            TargetException targetException = targetException1;
                            conversionException = targetException;
                        }
                    }
                    if (attrType.IsPrimitive && valType.IsPrimitive)
                    {
                        object[] str = new object[1];
                        str[0]    = valueObject.ToString();
                        objArray1 = str;
                        Type[] typeArray2 = new Type[1];
                        typeArray2[0] = typeof(string);
                        typeArray1    = typeArray2;
                    }
                    MethodInfo method = attrType.GetMethod("Parse", BindingFlags.Static | BindingFlags.Public, null, typeArray1, null);
                    if (method != null)
                    {
                        try
                        {
                            newValue = method.Invoke(null, objArray1);
                            flag     = true;
                            return(flag);
                        }
                        catch (TargetInvocationException targetInvocationException3)
                        {
                            TargetInvocationException targetInvocationException2 = targetInvocationException3;
                            conversionException = targetInvocationException2.InnerException;
                        }
                        catch (TargetException targetException3)
                        {
                            TargetException targetException2 = targetException3;
                            conversionException = targetException2;
                        }
                    }
                    return(false);
                }
                else
                {
                    newValue = valueObject.ToString();
                    return(true);
                }
            }
            else
            {
                if (!Utils.TryParseEnum(attrType, valueObject, out newValue))
                {
                    object[] objArray2 = new object[2];
                    objArray2[0]        = valueObject;
                    objArray2[1]        = attrType;
                    conversionException = new FormatException(string.Format(CultureInfo.CurrentCulture, StringResources.EnumConversionError, objArray2));
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
        }