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); }
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"); } }
public void CreateInstanceWithoutTargetInvocationException_WhenConstructorSucceeds_ReturnsInstance(bool includeArgs) { object result = ExceptionUtils.CreateInstanceWithoutTargetInvocationException(typeof(object), includeArgs ? Type.EmptyTypes : null); Assert.IsNotNull(result); }
public void CreateInstanceWithoutTargetInvocationException_WhenTypeIsNull_Throws() { ExceptionUtils.CreateInstanceWithoutTargetInvocationException(null, EmptyArray <object> .Instance); }