예제 #1
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());
        }
예제 #2
0
        /// <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));
        }
 private void CallValidateGenericArguments(Type genericMethodDefinition, params Type[] typeArguments)
 {
     GenericArgumentUtility.ValidateGenericArguments(genericMethodDefinition.GetGenericArguments(), typeArguments, "xxx");
 }