コード例 #1
0
        /// <summary>
        /// Attempts to find an extension method that matches the name and arguments based on scanning the Type's passed in
        /// to the findMethodsOnTypes parameter
        /// </summary>
        /// <param name="thisObject">The instance object to execute the extension method for</param>
        /// <param name="args"></param>
        /// <param name="name"></param>
        /// <param name="findMethodsOnTypes"></param>
        /// <returns></returns>
        internal static object FindAndExecuteExtensionMethod <T>(T thisObject,
                                                                 object[] args,
                                                                 string name,
                                                                 IEnumerable <Type> findMethodsOnTypes)
        {
            object result = null;

            //find known extension methods that match the first type in the list
            MethodInfo toExecute = null;

            foreach (var t in findMethodsOnTypes)
            {
                toExecute = ExtensionMethodFinder.FindExtensionMethod(t, args, name, false);
                if (toExecute != null)
                {
                    break;
                }
            }

            if (toExecute != null)
            {
                var genericArgs = (new[] { (object)thisObject }).Concat(args);
                result = toExecute.Invoke(null, genericArgs.ToArray());
            }
            else
            {
                throw new MissingMethodException();
            }
            return(result);
        }
コード例 #2
0
        /// <summary>
        /// Attempts to find an extension method that matches the name and arguments based on scanning the Type's passed in
        /// to the findMethodsOnTypes parameter
        /// </summary>
        /// <param name="runtimeCache"></param>
        /// <param name="thisObject">The instance object to execute the extension method for</param>
        /// <param name="args"></param>
        /// <param name="name"></param>
        /// <param name="findMethodsOnTypes"></param>
        /// <returns></returns>
        internal static object FindAndExecuteExtensionMethod <T>(
            IRuntimeCacheProvider runtimeCache,
            T thisObject,
            object[] args,
            string name,
            IEnumerable <Type> findMethodsOnTypes)
        {
            object result = null;

            //find known extension methods that match the first type in the list
            MethodInfo toExecute = null;

            foreach (var t in findMethodsOnTypes)
            {
                toExecute = ExtensionMethodFinder.FindExtensionMethod(runtimeCache, t, args, name, false);
                if (toExecute != null)
                {
                    break;
                }
            }

            if (toExecute != null)
            {
                var genericArgs = (new[] { (object)thisObject }).Concat(args);

                // else we'd get an exception w/ message "Late bound operations cannot
                // be performed on types or methods for which ContainsGenericParameters is true."
                // because MakeGenericMethod must be used to obtain an actual method that can run
                if (toExecute.ContainsGenericParameters)
                {
                    throw new InvalidOperationException("Method contains generic parameters, something's wrong.");
                }

                result = toExecute.Invoke(null, genericArgs.ToArray());
            }
            else
            {
                throw new MissingMethodException(typeof(T).FullName, name);
            }
            return(result);
        }