Esempio n. 1
0
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] arguments, out object result)
        {
            if (CallTarget == null)
            {
                result = null;
                return(false);
            }

            object[] theArguments = TypeFactorization.MaybeRenameArguments(binder.CallInfo, arguments);

            try
            {
                result = InvocationBinding.InvokeMember(CallTarget, binder.Name, theArguments);
            }
            catch (RuntimeBinderException)
            {
                result = null;
                try
                {
                    InvocationBinding.InvokeMemberAction(CallTarget, binder.Name, theArguments);
                }
                catch (RuntimeBinderException)
                {
                    return(false);
                }
            }
            return(true);
        }
Esempio n. 2
0
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            if (!base.TryInvokeMember(binder, args, out result))
            {
                result = InvocationBinding.InvokeGet(CallTarget, binder.Name);
                if (result == null)
                {
                    return(false);
                }
                var functor = result as Delegate;
                if (!binder.CallInfo.ArgumentNames.Any() && null != functor)
                {
                    try
                    {
                        result = this.InvokeMethodDelegate(functor, args);
                    }
                    catch (RuntimeBinderException)
                    {
                        return(false);
                    }
                }
                try
                {
                    result = InvocationBinding.Invoke(result,
                                                      TypeFactorization.MaybeRenameArguments(binder.CallInfo, args));
                }
                catch (RuntimeBinderException)
                {
                    return(false);
                }
            }

            return(this.WireUpForInterface(binder.Name, true, ref result));
        }
 public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
 {
     if (base.TryInvokeMember(binder, args, out result))
     {
         Recording.Add(new Invocation(InvocationKind.InvokeMemberUnknown, binder.Name,
                                      TypeFactorization.MaybeRenameArguments(binder.CallInfo, args)));
         return(true);
     }
     return(false);
 }
 public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
 {
     if (base.TryGetIndex(binder, indexes, out result))
     {
         Recording.Add(new Invocation(InvocationKind.GetIndex, Invocation.IndexBinderName,
                                      TypeFactorization.MaybeRenameArguments(binder.CallInfo, indexes)));
         return(true);
     }
     return(false);
 }
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            var          sk     = SignatureKey.Create(binder, args);
            SignatureKey nested = null;

            if (_nestedTargets.TryGetValue(sk, out nested))
            {
                var member = _dynamicMembers[nested];
                try
                {
                    result = InvocationBinding.InvokeMember(member, binder.Name, args);
                }
                catch (RuntimeBinderException)
                {
                    result = null;
                    return(false);
                }

                return(this.WireUpForInterface(binder.Name, true, ref result));
            }

            if (_dynamicMembers.TryGetValue(sk, out result))
            {
                var functor = result as Delegate;
                if (result == null)
                {
                    return(false);
                }
                if (!binder.CallInfo.ArgumentNames.Any() && functor != null)
                {
                    try
                    {
                        result = this.InvokeMethodDelegate(functor, args);
                    }
                    catch (RuntimeBinderException)
                    {
                        return(false);
                    }
                }
                else
                {
                    try
                    {
                        result = InvocationBinding.Invoke(result,
                                                          TypeFactorization.MaybeRenameArguments(binder.CallInfo, args));
                    }
                    catch (RuntimeBinderException)
                    {
                        return(false);
                    }
                }
                return(this.WireUpForInterface(binder.Name, true, ref result));
            }
            return(this.WireUpForInterface(binder.Name, false, ref result));
        }
 public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value)
 {
     if (base.TrySetIndex(binder, indexes, value))
     {
         var combinedArguments = indexes.Concat(new[] { value }).ToArray();
         Recording.Add(new Invocation(InvocationKind.GetIndex, Invocation.IndexBinderName,
                                      TypeFactorization.MaybeRenameArguments(binder.CallInfo, combinedArguments)));
         return(true);
     }
     return(false);
 }
Esempio n. 7
0
        public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
        {
            if (CallTarget == null)
            {
                result = null;
                return(false);
            }

            object[] theArguments = TypeFactorization.MaybeRenameArguments(binder.CallInfo, indexes);

            result = InvocationBinding.InvokeGetIndex(CallTarget, theArguments);
            return(true);
        }
        public static dynamic Invoke(object target, params object[] arguments)
        {
            string[] argumentNames;
            Type     context;
            bool     staticContext;

            target    = target.GetInvocationContext(out context, out staticContext);
            arguments = TypeFactorization.ExtractArgumentNamesAndValues(arguments, out argumentNames);
            CallSite theCallSite = null;

            return(InvocationMapping.InvokeDirectCallSite(target, arguments, argumentNames, context, staticContext,
                                                          ref theCallSite));
        }
        public static void InvokeMemberAction(object target, MemberInvocationMoniker name, params object[] arguments)
        {
            string[] argumentNames;
            Type     context;
            bool     staticContext;

            target    = target.GetInvocationContext(out context, out staticContext);
            arguments = TypeFactorization.ExtractArgumentNamesAndValues(arguments, out argumentNames);

            CallSite theCallSite = null;

            InvocationMapping.InvokeMemberActionCallSite(target, name, arguments, argumentNames, context, staticContext,
                                                         ref theCallSite);
        }
Esempio n. 10
0
        public static void InvokeSetIndex(object target, params object[] indexesThenValue)
        {
            string[] argNames;
            Type     context;
            bool     staticContext;

            target           = target.GetInvocationContext(out context, out staticContext);
            indexesThenValue = TypeFactorization.ExtractArgumentNamesAndValues(indexesThenValue, out argNames);

            CallSite theCallSite = null;

            InvocationMapping.InvokeSetIndexCallSite(target, indexesThenValue, argNames, context, staticContext,
                                                     ref theCallSite);
        }
Esempio n. 11
0
        public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value)
        {
            if (CallTarget == null)
            {
                return(false);
            }

            var combinedArguments = indexes.Concat(new[] { value }).ToArray();

            object[] tArgs = TypeFactorization.MaybeRenameArguments(binder.CallInfo, combinedArguments);

            InvocationBinding.InvokeSetIndex(CallTarget, tArgs);
            return(true);
        }
Esempio n. 12
0
        private void ExtractArguments(string[] argNames, object[] storedArgs)
        {
            _argumentNames = argNames ?? new string[] {};

            if (null != storedArgs)
            {
                _argumentCount = storedArgs.Length;
                string[] theArgumentNames;
                Arguments = TypeFactorization.ExtractArgumentNamesAndValues(storedArgs, out theArgumentNames);
                if (_argumentNames.Length < theArgumentNames.Length)
                {
                    _argumentNames = theArgumentNames;
                }
            }
        }
        public static bool IsActionOrFunc(object target)
        {
            if (target == null)
            {
                return(false);
            }

            var type = TypeFactorization.ForceTargetType(target);

            if (type.IsGenericType)
            {
                type = type.GetGenericTypeDefinition();
            }

            return(FuncPrototypeArgCount.ContainsKey(type) || ActionPrototypeArgCount.ContainsKey(type));
        }
Esempio n. 14
0
        private static object InvokeHelper(CallInfo callinfo, IList <object> args, Activate buildType = null)
        {
            bool   setWithName = true;
            object theArgument = null;

            if (callinfo.ArgumentNames.Count == 0 && callinfo.ArgumentCount == 1)
            {
                theArgument = args[0];

                if (TypeFactorization.IsTypeAnonymous(theArgument) ||
                    theArgument is IEnumerable <KeyValuePair <string, object> > )
                {
                    setWithName = false;
                }
            }

            if (setWithName && callinfo.ArgumentNames.Count != callinfo.ArgumentCount)
            {
                throw new ArgumentException("Requires argument names for every argument");
            }

            object result;

            if (buildType != null)
            {
                result = buildType.Create();
            }
            else
            {
                try
                {
                    result = Activator.CreateInstance <TObjectProtoType>();
                }
                catch (MissingMethodException)
                {
                    result = InvocationBinding.CreateInstance(typeof(TObjectProtoType));
                }
            }
            if (setWithName)
            {
                theArgument = callinfo.ArgumentNames.Zip(args, (n, a) => new KeyValuePair <string, object>(n, a));
            }

            return(InvocationBinding.InvokeSetAll(result, theArgument));
        }
Esempio n. 15
0
        public static dynamic CreateInstance(Type type, params object[] arguments)
        {
            string[] argumentNames;
            bool     isValue = type.IsValueType;

            if (isValue && arguments.Length == 0) //dynamic invocation doesn't see constructors of value types
            {
                return(Activator.CreateInstance(type));
            }

            arguments = TypeFactorization.ExtractArgumentNamesAndValues(arguments, out argumentNames);
            CallSite theCallSite = null;

            var context = type.MaybeDetectArrayContext();

            return(InvocationMapping.InvokeConstructorCallSite(type, isValue, arguments, argumentNames, context,
                                                               ref theCallSite));
        }
Esempio n. 16
0
        public static void InvokeAddAssign(object target, string name, object value)
        {
            CallSite callSiteAdd     = null;
            CallSite callSiteGet     = null;
            CallSite callSiteSet     = null;
            CallSite callSiteIsEvent = null;

            Type context;
            bool staticContext;

            target = target.GetInvocationContext(out context, out staticContext);

            var args = new[] { value };

            string[] argNames;
            args = TypeFactorization.ExtractArgumentNamesAndValues(args, out argNames);

            InvocationMapping.InvokeAddAssignCallSite(target, name, args, argNames, context, staticContext,
                                                      ref callSiteIsEvent, ref callSiteAdd, ref callSiteGet,
                                                      ref callSiteSet);
        }
Esempio n. 17
0
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] arguments, out object result)
        {
            var invocation = new Invocation(InvocationKind.InvokeMemberUnknown, binder.Name,
                                            TypeFactorization.MaybeRenameArguments(binder.CallInfo, arguments));

            var before = _aspectWeaver.Before(binder);
            var ok     = true;

            result = null;

            foreach (var aspect in before)
            {
                ok = ok && aspect.InterceptBefore(invocation, OriginalTarget, _aspectExtensions, out result);
            }

            var instead = _aspectWeaver.InsteadOf(binder);

            if (instead.Any())
            {
                foreach (var aspect in instead)
                {
                    ok = ok && aspect.InterceptInstead(invocation, OriginalTarget, _aspectExtensions, out result);
                }
            }
            else
            {
                ok = ok && base.TryInvokeMember(binder, invocation.Arguments, out result);
            }

            var after = _aspectWeaver.After(binder);

            foreach (var aspect in after)
            {
                ok = ok && aspect.InterceptAfter(invocation, OriginalTarget, _aspectExtensions, out result);
            }

            return(ok);
        }
Esempio n. 18
0
        public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
        {
            IEnumerable <KeyValuePair <string, object> > kvps = null;
            object target = null;

            result = null;

            //Setup Properties as dictionary
            if (binder.CallInfo.ArgumentNames.Any())
            {
                if (binder.CallInfo.ArgumentNames.Count + 1 == binder.CallInfo.ArgumentCount)
                {
                    target = args.First();
                    kvps   = binder.CallInfo
                             .ArgumentNames
                             .Zip(args.Skip(1), (key, value) => new { key, value })
                             .ToDictionary(k => k.key, v => v.value);
                }
                else
                {
                    throw new RuntimeBinderException(
                              "InvokeSetAll requires first parameter to be target unamed, and all other parameters to be named.");
                }
            }
            else if (args.Length == 2)
            {
                target = args[0];
                if (args[1] is IEnumerable <KeyValuePair <string, object> > )
                {
                    kvps = (IEnumerable <KeyValuePair <string, object> >)args[1];
                }
                else if (args[1] is IEnumerable &&
                         args[1].GetType().IsGenericType
                         )
                {
                    var enArgs = (IEnumerable)args[1];

                    var tInterface = enArgs.GetType().GetInterface("IEnumerable`1", false);
                    if (tInterface != null)
                    {
                        var tParamTypes = tInterface.GetGenericArguments();
                        if (tParamTypes.Length == 1 &&
                            tParamTypes[0].GetGenericTypeDefinition() == typeof(Tuple <,>))
                        {
                            kvps = enArgs.Cast <dynamic>().ToDictionary(k => (string)k.Item1, v => v.Item2);
                        }
                    }
                }
                else if (TypeFactorization.IsTypeAnonymous(args[1]))
                {
                    var keyDict = new Dictionary <string, object>();
                    foreach (var tProp in args[1].GetType().GetProperties())
                    {
                        keyDict[tProp.Name] = InvocationBinding.InvokeGet(args[1], tProp.Name);
                    }
                    kvps = keyDict;
                }
            }
            //Invoke all properties
            if (target != null && kvps != null)
            {
                foreach (var pair in kvps)
                {
                    InvocationBinding.InvokeSetChain(target, pair.Key, pair.Value);
                }
                result = target;
                return(true);
            }
            return(false);
        }
 public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
 {
     result = InvocationBinding.CreateInstance(_type,
                                               TypeFactorization.MaybeRenameArguments(binder.CallInfo, args));
     return(true);
 }