Exemple #1
0
        /// <summary>
        /// 根据实参参数类型推断当前泛型方法定义的类型参数,
        /// 并返回表示结果封闭构造方法的 <see cref="MethodInfo"/> 对象。
        /// </summary>
        /// <param name="method">要进行类型参数推断的泛型方法定义。</param>
        /// <param name="types">泛型方法的实参参数数组。</param>
        /// <returns>一个 <see cref="MethodInfo"/> 对象,表示通过将当前泛型方法定义的类型参数替换为根据
        /// <paramref name="types"/> 推断得到的元素生成的封闭构造方法。</returns>
        /// <exception cref="InvalidOperationException">当前 <see cref="MethodInfo"/> 不表示泛型方法定义。
        /// 也就是说,<see cref="MethodBase.IsGenericMethodDefinition "/> 返回 <c>false</c>。</exception>
        /// <exception cref="ArgumentNullException"><paramref name="method"/> 为 <c>null</c>。</exception>
        /// <exception cref="ArgumentNullException"><paramref name="types"/> 为 <c>null</c>。</exception>
        /// <exception cref="ArgumentException">不能从 <paramref name="types"/> 推断得到类型参数。</exception>
        /// <exception cref="ArgumentException">根据 <paramref name="types"/>
        /// 推断出来的类型参数中的某个元素不满足为当前泛型方法定义的相应类型参数指定的约束。</exception>
        /// <overloads>
        /// <summary>
        /// 根据实参参数类型推断当前泛型方法定义的类型参数,
        /// 并返回表示结果封闭构造方法的 <see cref="MethodInfo"/> 对象。
        /// </summary>
        /// </overloads>
        public static MethodInfo MakeGenericMethodFromParamTypes(this MethodInfo method, params Type[] types)
        {
            CommonExceptions.CheckArgumentNull(method, "method");
            CommonExceptions.CheckArgumentNull(types, "types");
            Contract.Ensures(Contract.Result <MethodInfo>() != null);
            if (!method.IsGenericMethodDefinition)
            {
                throw CommonExceptions.NeedGenericMethodDefinition("method");
            }
            var result = GenericArgumentsInferences(method, null, types, MethodArgumentsOption.OptionalParamBinding);

            if (result == null)
            {
                throw CommonExceptions.CannotInferenceGenericArguments("method");
            }
            return(method.MakeGenericMethod(result.GenericArguments));
        }