private MethodInfo Bind(GenericArgumentBinder binder, MethodInfo method, Type descriptorType)
        {
            MethodInfo bindedMethod;

            if (binder.TryBind(method, new[] { descriptorType }, out bindedMethod))
            {
                return(bindedMethod);
            }

            return(null);
        }
示例#2
0
        public static void Main(string[] args)
        {
            GenericArgumentBinder binder = new GenericArgumentBinder();

            MethodInfo[] methods;
            MethodInfo   method;

            binder.TryBind(typeof(MyClass).GetMethod("MyMethod2"),
                           new Type[] { typeof(MyClass2[]) },
                           out methods);

            int[] array = {};
            MyStaticClass.NewTest <object>(array);
        }
示例#3
0
        public MethodInfo GetMethod(Type processorType, IStateMember member, Type runtimeType, bool routed)
        {
            Type descriptorType = DescriptorExtensions.GetDescriptorType(member, runtimeType);

            // TODO: First implementation: reimplement this.
            GenericArgumentBinder binder =
                new GenericArgumentBinder
                    (new FallbackToFirstCandidateMethodSelector
                        (new BinderMethodSelector(Type.DefaultBinder)));

            // TODO: Return this after processors are fixed.
            // IEnumerable<Type> typesToSearch = processorType.WithInterfaces();
            IEnumerable <Type> typesToSearch = processorType.AsEnumerable();

            IEnumerable <MethodInfo> methods =
                typesToSearch.SelectMany(x => x.GetMethods())
                .Where(x => x.IsDefined(typeof(ProcessorMethodAttribute)))
                .Where(x => !x.GetCustomAttribute <ProcessorMethodAttribute>().OnlyOnRoute ||
                       routed);

            IEnumerable <IGrouping <int, MethodInfo> > candidates =
                methods.Select(method => Bind(binder, method, descriptorType))
                .Where(x => x != null)
                .GroupBy(x => x.GetCustomAttribute <ProcessorMethodAttribute>().Precedence)
                .OrderBy(x => x.Key);

            IGrouping <int, MethodInfo> maximum = candidates.FirstOrDefault();

            if (maximum.Skip(1).Any())
            {
                throw new Exception("Too many candidates for the processor thingy. Use precedence.");
            }
            else
            {
                MethodInfo method = maximum.FirstOrDefault();

                return(method);
            }
        }
        private void CallMatchedProcess(IReflectionValueDescriptor descriptor, bool routed)
        {
            GenericArgumentBinder binder = new GenericArgumentBinder(new FallbackToFirstCandidateMethodSelector(new BinderMethodSelector(Type.DefaultBinder)));

            IEnumerable <MethodInfo> methods =
                _processor.GetType().GetMethods()
                .Where(x => x.IsDefined(typeof(ProcessorMethodAttribute)))
                .Where(x => !x.GetCustomAttribute <ProcessorMethodAttribute>().OnlyOnRoute || routed);

            Type descriptorType = descriptor.GetType();

            IEnumerable <IGrouping <int, MethodInfo> > candidates =
                methods.Select(method => Bind(binder, method, descriptorType))
                .Where(x => x != null)
                .GroupBy(x => x.GetCustomAttribute <ProcessorMethodAttribute>().Precedence)
                .OrderBy(x => x.Key);

            IGrouping <int, MethodInfo> maximum = candidates.FirstOrDefault();

            if (maximum.Skip(1).Any())
            {
                throw new Exception("Too many candidates for the processor thingy. Use precedence.");
            }
            else
            {
                MethodInfo method = maximum.FirstOrDefault();

                if (method != null)
                {
                    method.Invoke(_processor, new object[] { descriptor });
                }
                else
                {
                    throw new Exception(
                              "No method found :(, this might be a bug in your code, or in the GraphClimber. Good luck.");
                }
            }
        }