public void Instantiate_RuntimeTypeArgument() { var runtimeType = ReflectionObjectMother.GetSomeType(); var info = new TypeInstantiationInfo(_genericTypeDefinition, new[] { runtimeType }); var result = _context.Instantiate(info); Assert.That(result.IsRuntimeType(), Is.True); Assert.That(result.GetGenericTypeDefinition(), Is.SameAs(_genericTypeDefinition)); Assert.That(result.GetGenericArguments(), Is.EqualTo(new[] { runtimeType })); }
public void Initialization_ReadOnlyAndWriteOnlyProperty() { var info1 = new TypeInstantiationInfo(typeof(GenericTypeWithProperties <>), new Type[] { _customType }); var instantiation = new TypeInstantiation(info1, new TypeInstantiationContext()); var property1 = instantiation.GetProperty("ReadOnlyProperty"); var property2 = instantiation.GetProperty("WriteOnlyProperty"); Assert.That(property1.GetSetMethod(true), Is.Null); Assert.That(property2.GetGetMethod(true), Is.Null); }
public void Instantiate_CustomGenericTypeDefinition() { var typeParameter = ReflectionObjectMother.GetSomeGenericParameter(); var customGenericTypeDefinition = CustomTypeObjectMother.Create(typeArguments: new[] { typeParameter }); var instantiationInfo = new TypeInstantiationInfo(customGenericTypeDefinition, new[] { _customType }); var result = _context.Instantiate(instantiationInfo); Assert.That(result, Is.TypeOf <TypeInstantiation>()); Assert.That(result.GetGenericTypeDefinition(), Is.SameAs(customGenericTypeDefinition)); Assert.That(result.GetGenericArguments(), Is.EqualTo(new[] { _customType })); }
public void SetUp() { _context = new TypeInstantiationContext(); _genericTypeDefinition = typeof(List <>); _customType = CustomTypeObjectMother.Create(); _info = new TypeInstantiationInfo(_genericTypeDefinition, new[] { _customType }.AsOneTime()); _parameter = typeof(GenericType <>).GetGenericArguments().Single(); _argument = ReflectionObjectMother.GetSomeType(); _parametersToArguments = new Dictionary <Type, Type> { { _parameter, _argument } }; }
public void IntegrationOfInitializationAndSubstitution_RecursiveGenericInBaseType() { var genericRuntimeType = typeof(RecursiveGenericType <int>); var genericBaseRuntimeType = typeof(BaseType <RecursiveGenericType <int> >); Assert.That(genericRuntimeType, Is.SameAs(genericBaseRuntimeType.GetGenericArguments().Single()), "Assert original reflection behavior."); var genericTypeDefinition = typeof(RecursiveGenericType <>); var typeArguments = new Type[] { _customType }; var info = new TypeInstantiationInfo(genericTypeDefinition, typeArguments); var instantiation = new TypeInstantiation(info, new TypeInstantiationContext()); Assertion.IsNotNull(instantiation.BaseType); Assert.That(instantiation, Is.SameAs(instantiation.BaseType.GetGenericArguments().Single())); }
public void Equals_Object() { var info1 = new TypeInstantiationInfo(_genericTypeDefinition, _typeArguments.Reverse()); var info2 = new TypeInstantiationInfo(_genericTypeDefinition, _typeArguments); Assert.That(info1, Is.Not.EqualTo(_instantiationInfo)); Assert.That(info2, Is.EqualTo(_instantiationInfo)); var instantiation1 = new TypeInstantiation(info1, new TypeInstantiationContext()); var instantiation2 = new TypeInstantiation(info2, new TypeInstantiationContext()); Assert.That(_instantiation.Equals((object)null), Is.False); Assert.That(_instantiation.Equals(new object()), Is.False); Assert.That(_instantiation.Equals((object)instantiation1), Is.False); Assert.That(_instantiation.Equals((object)instantiation2), Is.True); }
public void Initialization_MemberInitializationIsLazy_AndUsesAllBindingFlagsToRetrieveMembers() { var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static; var nestedTypes = _genericTypeDefinition.GetNestedTypes(bindingFlags); var fields = _genericTypeDefinition.GetFields(bindingFlags); var ctors = _genericTypeDefinition.GetConstructors(bindingFlags); var methods = _genericTypeDefinition.GetMethods(bindingFlags); var properties = _genericTypeDefinition.GetProperties(bindingFlags); var events = _genericTypeDefinition.GetEvents(bindingFlags); var memberSelectorMock = new Mock <IMemberSelector> (MockBehavior.Strict); var typeParameters = new[] { ReflectionObjectMother.GetSomeType() }; var genericTypeDefinition = CustomTypeObjectMother.Create(memberSelectorMock.Object, typeArguments: typeParameters, nestedTypes: nestedTypes, fields: fields, constructors: ctors, methods: methods, properties: properties, events: events); // TODO 5816 //memberSelectorMock.Setup (mock => mock.SelectTypes (nestedTypes, bindingFlags)).Returns (nestedTypes); memberSelectorMock.Setup(mock => mock.SelectFields(fields, bindingFlags, genericTypeDefinition)).Returns(fields).Verifiable(); memberSelectorMock.Setup(mock => mock.SelectMethods(ctors, bindingFlags, genericTypeDefinition)).Returns(ctors).Verifiable(); // Note: GetMethods is optimized for retrieving all the methods; so there is no memberSelectorMock call. memberSelectorMock.Setup(mock => mock.SelectProperties(properties, bindingFlags, genericTypeDefinition)).Returns(properties).Verifiable(); memberSelectorMock.Setup(mock => mock.SelectEvents(events, bindingFlags, genericTypeDefinition)).Returns(events).Verifiable(); var typeArguments = new[] { ReflectionObjectMother.GetSomeType() }; var info = new TypeInstantiationInfo(genericTypeDefinition, typeArguments); var typeInstantiation = new TypeInstantiation(info, new TypeInstantiationContext()); // Evaluation is lazy. memberSelectorMock.Verify(mock => mock.SelectTypes(nestedTypes, bindingFlags), Times.Never()); memberSelectorMock.Verify(mock => mock.SelectFields(fields, bindingFlags, genericTypeDefinition), Times.Never()); memberSelectorMock.Verify(mock => mock.SelectMethods(ctors, bindingFlags, genericTypeDefinition), Times.Never()); // Note: GetMethods is optimized for retrieving all the methods; so there is no memberSelectorMock call. memberSelectorMock.Verify(mock => mock.SelectProperties(properties, bindingFlags, genericTypeDefinition), Times.Never()); memberSelectorMock.Verify(mock => mock.SelectEvents(events, bindingFlags, genericTypeDefinition), Times.Never()); // Trigger instantiation. // TODO 5816 //Dev.Null = typeInstantiation.GetNestedTypes(); Dev.Null = typeInstantiation.GetFields(); Dev.Null = typeInstantiation.GetConstructors(); Dev.Null = typeInstantiation.GetMethods(); Dev.Null = typeInstantiation.GetProperties(); Dev.Null = typeInstantiation.GetEvents(); memberSelectorMock.Verify(); }
public void Equals_Type() { var info1 = new TypeInstantiationInfo(_genericTypeDefinition, _typeArguments.Reverse()); var info2 = new TypeInstantiationInfo(_genericTypeDefinition, _typeArguments); Assert.That(info1, Is.Not.EqualTo(_instantiationInfo)); Assert.That(info2, Is.EqualTo(_instantiationInfo)); var instantiation1 = new TypeInstantiation(info1, new TypeInstantiationContext()); var instantiation2 = new TypeInstantiation(info2, new TypeInstantiationContext()); // ReSharper disable CheckForReferenceEqualityInstead.1 Assert.That(_instantiation.Equals(null), Is.False); // ReSharper restore CheckForReferenceEqualityInstead.1 Assert.That(_instantiation.Equals(instantiation1), Is.False); Assert.That(_instantiation.Equals(instantiation2), Is.True); }
public static TypeInstantiation Create( Type genericTypeDefinition = null, IEnumerable <Type> typeArguments = null, TypeInstantiationContext instantiationContext = null, IMemberSelector memberSelector = null) { genericTypeDefinition = genericTypeDefinition ?? typeof(MyGenericType <>); typeArguments = typeArguments ?? genericTypeDefinition.GetGenericArguments().Select(a => ReflectionObjectMother.GetSomeType()); var instantiationInfo = new TypeInstantiationInfo(genericTypeDefinition, typeArguments); instantiationContext = instantiationContext ?? new TypeInstantiationContext(); memberSelector = memberSelector ?? new MemberSelector(new BindingFlagsEvaluator()); var typeInstantiation = new TypeInstantiation(instantiationInfo, instantiationContext); typeInstantiation.SetMemberSelector(memberSelector); return(typeInstantiation); }
/// <summary> /// Substitutes the type parameters of the generic type definition and returns a <see cref="Type"/> object representing the resulting /// constructed generic type. Use this as a replacement for <see cref="Type.MakeGenericType"/>. /// </summary> /// <param name="genericTypeDefinition">The generic type definition.</param> /// <param name="typeArguments">The type arguments.</param> /// <returns>The generic type instantiation.</returns> public static Type MakeTypePipeGenericType(this Type genericTypeDefinition, params Type[] typeArguments) { ArgumentUtility.CheckNotNull("typeArguments", typeArguments); ArgumentUtility.CheckNotNullOrItemsNull("typeArguments", typeArguments); if (!genericTypeDefinition.IsGenericTypeDefinition) { var message = string.Format( "'{0}' is not a generic type definition. {1} may only be called on a type for which Type.IsGenericTypeDefinition is true.", genericTypeDefinition.Name, MethodInfo.GetCurrentMethod().Name); throw new InvalidOperationException(message); } var typeParameters = genericTypeDefinition.GetGenericArguments(); GenericArgumentUtility.ValidateGenericArguments(typeParameters, typeArguments, genericTypeDefinition.Name); var instantiationContext = new TypeInstantiationContext(); var instantiationInfo = new TypeInstantiationInfo(genericTypeDefinition, typeArguments); return(instantiationContext.Instantiate(instantiationInfo)); }