Exemplo n.º 1
0
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            // Get type args of the call
            Type[] typeArgs = ExposedObjectHelper.GetTypeArgs(binder);
            if (typeArgs != null && typeArgs.Length == 0)
            {
                typeArgs = null;
            }

            // Try to call a non-generic instance method
            if (typeArgs == null &&
                staticMethods.ContainsKey(binder.Name) &&
                staticMethods[binder.Name].ContainsKey(args.Length) &&
                ExposedObjectHelper.InvokeBestMethod(args, null, staticMethods[binder.Name][args.Length], out result))
            {
                return(true);
            }

            // Try to call a generic instance method
            if (staticMethods.ContainsKey(binder.Name) &&
                staticMethods[binder.Name].ContainsKey(args.Length))
            {
                var methods = (from method in staticGenericMethods[binder.Name][args.Length]
                               where method.GetGenericArguments().Length == typeArgs.Length
                               select method.MakeGenericMethod(typeArgs)).ToList();

                if (ExposedObjectHelper.InvokeBestMethod(args, null, methods, out result))
                {
                    return(true);
                }
            }

            result = null;
            return(false);
        }
Exemplo n.º 2
0
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            // Get type args of the call
            Type[] typeArgs = ExposedObjectHelper.GetTypeArgs(binder);
            if (typeArgs != null && typeArgs.Length == 0)
            {
                typeArgs = null;
            }


            // Try to call a non-generic instance method
            if (typeArgs == null &&
                instanceMethods.ContainsKey(binder.Name) &&
                instanceMethods[binder.Name].ContainsKey(args.Length) &&
                ExposedObjectHelper.InvokeBestMethod(args, objectInstance, instanceMethods[binder.Name][args.Length], out result))
            {
                return(true);
            }


            // Try to call a generic instance method
            if (instanceMethods.ContainsKey(binder.Name) &&
                instanceMethods[binder.Name].ContainsKey(args.Length))
            {
                List <MethodInfo> methods = null;

                try
                {
                    methods = (from method in genInstanceMethods[binder.Name][args.Length]
                               where method.GetGenericArguments().Length == typeArgs.Length
                               select method.MakeGenericMethod(typeArgs)).ToList();
                }
                catch (Exception)
                {
                    // Nicht generisch sondern overload
                    methods = new List <MethodInfo>();
                    foreach (List <MethodInfo> methodInfos in instanceMethods.Where(pair => pair.Key == binder.Name).Select(pair => pair.Value).SelectMany(dictionary => dictionary.Values))
                    {
                        methods.AddRange(methodInfos);
                    }
                }

                if (ExposedObjectHelper.InvokeBestMethod(args, objectInstance, methods, out result))
                {
                    return(true);
                }
            }

            result = null;
            return(false);
        }