public void Instantiate_CustomTypeArgument()
        {
            var result = _infoWithCustomType.Instantiate();

            Assert.That(result, Is.TypeOf <MethodInstantiation>());
            Assert.That(result.GetGenericMethodDefinition(), Is.EqualTo(_infoWithCustomType.GenericMethodDefinition));
            Assert.That(result.GetGenericArguments(), Is.EqualTo(_infoWithCustomType.TypeArguments));
        }
        public void Instantiate_RuntimeTypeArgument()
        {
            var result = _infoWithRuntimeType.Instantiate();

            Assert.That(result.GetType().FullName, Is.EqualTo("System.Reflection.RuntimeMethodInfo"));
            Assert.That(result.GetGenericMethodDefinition(), Is.EqualTo(_infoWithRuntimeType.GenericMethodDefinition));
            Assert.That(result.GetGenericArguments(), Is.EqualTo(_infoWithRuntimeType.TypeArguments));
        }
        public void Instantiate_CustomGenericMethodDefinition()
        {
            var typeParameter = ReflectionObjectMother.GetSomeGenericParameter();
            var customGenericMethodDefinition = CustomMethodInfoObjectMother.Create(typeArguments: new[] { typeParameter });
            var instantiationInfo             = new MethodInstantiationInfo(customGenericMethodDefinition, new[] { _runtimeType });

            var result = instantiationInfo.Instantiate();

            Assert.That(result, Is.TypeOf <MethodInstantiation>());
            Assert.That(result.GetGenericMethodDefinition(), Is.EqualTo(instantiationInfo.GenericMethodDefinition));
            Assert.That(result.GetGenericArguments(), Is.EqualTo(instantiationInfo.TypeArguments));
        }
Beispiel #4
0
        /// <summary>
        /// Substitutes the type parameters of the generic type definition and returns a <see cref="MethodInfo"/> object representing the resulting
        /// constructed method. Use this as a replacement for <see cref="MethodInfo.MakeGenericMethod"/>.
        /// </summary>
        /// <param name="genericMethodDefinition">The generic method definition.</param>
        /// <param name="typeArguments">The type arguments.</param>
        /// <returns>The generic method instantiation.</returns>
        public static MethodInfo MakeTypePipeGenericMethod(this MethodInfo genericMethodDefinition, params Type[] typeArguments)
        {
            ArgumentUtility.CheckNotNull("genericMethodDefinition", genericMethodDefinition);
            ArgumentUtility.CheckNotNullOrItemsNull("typeArguments", typeArguments);

            if (!genericMethodDefinition.IsGenericMethodDefinition)
            {
                var message = string.Format(
                    "'{0}' is not a generic method definition. MakeTypePipeGenericMethod may only be called on a method for which "
                    + "MethodInfo.IsGenericMethodDefinition is true.",
                    genericMethodDefinition.Name);
                throw new InvalidOperationException(message);
            }

            var typeParameters = genericMethodDefinition.GetGenericArguments();

            GenericArgumentUtility.ValidateGenericArguments(typeParameters, typeArguments, genericMethodDefinition.Name);

            var instantiationInfo = new MethodInstantiationInfo(genericMethodDefinition, typeArguments);

            return(instantiationInfo.Instantiate());
        }