public void CreateType(Type instanceType, Type baseType) { // Arrange Func <object> instanceDelegate = TypeActivator.Create(instanceType); // Act object instance = instanceDelegate(); // Assert Assert.IsType(instanceType, instance); }
public void CreateOfTInvalid() { // string doesn't have a default ctor Assert.ThrowsArgument(() => TypeActivator.Create <string>(), paramName: null); // Uri doesn't have a default ctor Assert.ThrowsArgument(() => TypeActivator.Create <Uri>(), paramName: null); // HttpContent is abstract Assert.Throws <InvalidOperationException>(() => TypeActivator.Create <HttpContent>()); // HttpActionDescriptor is abstract Assert.Throws <InvalidOperationException>(() => TypeActivator.Create <HttpActionDescriptor>()); }
public void CreateOfTBaseInvalid() { // int not being a ref type Assert.ThrowsArgument(() => TypeActivator.Create <object>(typeof(int)), paramName: null); // GUID is not a ref type Assert.ThrowsArgument(() => TypeActivator.Create <object>(typeof(Guid)), paramName: null); // HttpStatusCode is not a ref type Assert.ThrowsArgument(() => TypeActivator.Create <object>(typeof(HttpStatusCode)), paramName: null); // string does not have a default ctor Assert.ThrowsArgument(() => TypeActivator.Create <string>(typeof(string)), paramName: null); // ObjectContent does not have a default ctor Assert.ThrowsArgument(() => TypeActivator.Create <HttpContent>(typeof(ObjectContent)), paramName: null); // Base type and instance type flipped Assert.ThrowsArgument(() => TypeActivator.Create <ReflectedHttpActionDescriptor>(typeof(HttpActionDescriptor)), paramName: null); }
public void CreateTypeInvalidThrowsInvalidOperation(Type type) { // Abstract types cause InvalidOperationException Assert.Throws <InvalidOperationException>(() => TypeActivator.Create(type)); }
public void CreateTypeInvalidThrowsInvalidArgument(Type type) { // Value types, interfaces, and open generics cause ArgumentException Assert.ThrowsArgument(() => TypeActivator.Create(type), paramName: null); }