Esempio n. 1
0
        public Option <object> Invoke(Option <string> methodName, params object[] arguments)
        {
            Option <MethodInfo> bestMatch;

            // Search the instance methods
            var finder = new MethodBaseFinder <MethodInfo>();

            bestMatch = finder.GetBestMatch(_instanceMethodPool, new MethodFinderContext(methodName, arguments));

            if (bestMatch.HasValue)
            {
                return(bestMatch.Invoke(Option.Some(_target), arguments));
            }

            // Fall back to the extension methods if it isn't found
            var adjustedArguments = new List <object> {
                _target
            };

            adjustedArguments.AddRange(arguments);

            var context = new MethodFinderContext(methodName, adjustedArguments);

            bestMatch = finder.GetBestMatch(_extensionMethodPool, context);

            return(bestMatch.Invoke(Option.None <object>(), adjustedArguments.ToArray()));
        }
Esempio n. 2
0
        public static Option <object> InvokeStatic(this Type targetType, string methodName, params object[] args)
        {
            if (targetType == null)
            {
                throw new ArgumentNullException(nameof(targetType));
            }

            var methods = targetType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
            var context = new MethodFinderContext(Option.Some(methodName), args);
            var finder  = new MethodBaseFinder <MethodInfo>();

            var matchingMethod = finder.GetBestMatch(methods, context);

            if (!matchingMethod.HasValue)
            {
                throw new MethodNotFoundException(methodName, args);
            }

            return(matchingMethod.Invoke(Option.None <object>(), args));
        }
Esempio n. 3
0
 public static bool HasMatchingMethods <TMethod>(this MethodBaseFinder <TMethod> finder,
                                                 IEnumerable <TMethod> methods, IMethodFinderContext context)
     where TMethod : MethodBase
 {
     return(finder.GetBestMatch(methods, context).HasValue);
 }