예제 #1
0
        public object CreateInstance()
        {
            object instance;

            if (resolvedConstructor != null)
            {
                instance = ExceptionUtils.InvokeConstructorWithoutTargetInvocationException(resolvedConstructor,
                                                                                            resolvedConstructorArguments);
            }
            else if (resolvedType.IsValueType)
            {
                instance = ExceptionUtils.CreateInstanceWithoutTargetInvocationException(resolvedType, null);
            }
            else
            {
                throw new InvalidOperationException(String.Format("Type '{0}' cannot be instantiated.", resolvedType));
            }

            foreach (KeyValuePair <FieldInfo, object> fieldValue in resolvedFieldValues)
            {
                fieldValue.Key.SetValue(instance, fieldValue.Value);
            }

            foreach (KeyValuePair <PropertyInfo, object> propertyValue in resolvedPropertyValues)
            {
                propertyValue.Key.SetValue(instance, propertyValue.Value, null);
            }

            return(instance);
        }
예제 #2
0
 public void CreateInstanceWithoutTargetInvocationException_WhenConstructorThrows_ShouldNotWrapTheException(bool includeArgs)
 {
     try
     {
         ExceptionUtils.CreateInstanceWithoutTargetInvocationException(typeof(ThrowBoomWhenConstructed), includeArgs ? Type.EmptyTypes : null);
         Assert.Fail("Should have thrown an InvalidOperationException");
     }
     catch (InvalidOperationException ex)
     {
         Assert.Contains(ex.StackTrace, "ThrowBoom");
     }
 }
예제 #3
0
        public void CreateInstanceWithoutTargetInvocationException_WhenConstructorSucceeds_ReturnsInstance(bool includeArgs)
        {
            object result = ExceptionUtils.CreateInstanceWithoutTargetInvocationException(typeof(object), includeArgs ? Type.EmptyTypes : null);

            Assert.IsNotNull(result);
        }
예제 #4
0
 public void CreateInstanceWithoutTargetInvocationException_WhenTypeIsNull_Throws()
 {
     ExceptionUtils.CreateInstanceWithoutTargetInvocationException(null, EmptyArray <object> .Instance);
 }