Exemplo n.º 1
0
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            if (CallTarget == null)
            {
                return(false);
            }

            if (InvocationBinding.InvokeIsEvent(CallTarget, binder.Name) && value is InterceptorAddRemove)
            {
                var theValue = value as InterceptorAddRemove;

                if (theValue.IsAdding)
                {
                    InvocationBinding.InvokeAddAssign(CallTarget, binder.Name, theValue.Delegate);
                }
                else
                {
                    InvocationBinding.InvokeSubtractAssign(CallTarget, binder.Name, theValue.Delegate);
                }

                return(true);
            }

            InvocationBinding.InvokeSet(CallTarget, binder.Name, value);

            return(true);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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));
        }
Exemplo n.º 4
0
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            Type type;

            Activate buildType;

            if (!_buildType.TryGetValue(binder.Name, out buildType))
            {
                buildType = null;
            }

            if (buildType == null && !_buildType.TryGetValue("Object", out buildType))
            {
                buildType = null;
            }

            result = InvokeHelper(binder.CallInfo, args, buildType);
            if (GetTypeForPropertyNameFromInterface(binder.Name, out type))
            {
                if (type.IsInterface && result != null && !type.IsAssignableFrom(result.GetType()))
                {
                    result = InvocationBinding.DynamicDressedAs(result, type);
                }
            }
            return(true);
        }
Exemplo n.º 5
0
        public object GetRealObject(StreamingContext context)
        {
            var interfaces = Interfaces ?? MonoInterfaces.Select(it => Type.GetType(it)).ToArray();
            var type       = BuildProxy.BuildType(Context, interfaces.First(), interfaces.Skip(1).ToArray());

            return(InvocationBinding.InitializeProxy(type, Original, interfaces));
        }
        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));
        }
Exemplo n.º 7
0
 public override IEnumerable <string> GetDynamicMemberNames()
 {
     if (!KnownInterfaces.Any())
     {
         var dynamicMemberNames = InvocationBinding.GetMemberNames(CallTarget, dynamicOnly: true);
         if (!dynamicMemberNames.Any())
         {
             return(InvocationBinding.GetMemberNames(CallTarget));
         }
     }
     return(base.GetDynamicMemberNames());
 }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
        private static object InterfaceProjection(Type theType, object itf)
        {
            if (itf is IDictionary <string, object> && !(itf is AbstractShapeableExpando))
            {
                itf = new ShapeableExpando((IDictionary <string, object>)itf);
            }
            else
            {
                itf = new DynamicPropertiesToReflectablePropertiesProxy(itf);
            }

            itf = InvocationBinding.DynamicDressedAs(itf, theType);
            return(itf);
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
0
        public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
        {
            IEnumerable <string> methodNames = new string[] {};

            if (OverrideGettingItemMethodNames != null)
            {
                methodNames = OverrideGettingItemMethodNames(this);
            }
            else
            {
                methodNames = InvocationBinding.GetMemberNames(GetRepresentedItem(), dynamicOnly: true);
            }

            return(new PropertyDescriptorCollection(methodNames.Select(it => new MetaProperty(it)).ToArray()));
        }
Exemplo n.º 12
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));
        }
Exemplo n.º 13
0
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (CallTarget == null)
            {
                result = null;
                return(false);
            }

            if (InvocationBinding.InvokeIsEvent(CallTarget, binder.Name))
            {
                result = new InterceptorAddRemove();
                return(true);
            }

            result = InvocationBinding.InvokeGet(CallTarget, binder.Name);

            return(true);
        }
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            object tOldValue;

            var          sk     = SignatureKey.Create(binder);
            SignatureKey nested = null;

            if (_nestedTargets.TryGetValue(sk, out nested))
            {
                var member = _dynamicMembers[nested];
                InvocationBinding.InvokeSet(member, binder.Name, value);
            }
            else if (!_dynamicMembers.TryGetValue(sk, out tOldValue) || value != tOldValue)
            {
                _dynamicMembers[sk] = value;
            }

            return(true);
        }
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            var          sk     = SignatureKey.Create(binder);
            SignatureKey nested = null;

            if (_nestedTargets.TryGetValue(sk, out nested))
            {
                var member = _dynamicMembers[nested];
                result = InvocationBinding.InvokeGet(member, binder.Name);
                return(this.WireUpForInterface(binder.Name, true, ref result));
            }

            if (_dynamicMembers.TryGetValue(sk, out result))
            {
                return(this.WireUpForInterface(binder.Name, true, ref result));
            }

            result = null;
            return(this.WireUpForInterface(binder.Name, false, ref result));
        }
Exemplo n.º 16
0
        private static object WireUpForInterfaceUsingType(bool gotType, Type theType, ShapeableObject bindSite,
                                                          object itf)
        {
            if (IsDictionaryButNotExpando(itf, gotType, theType))
            {
                itf = new ShapeableExpando((IDictionary <string, object>)itf);
            }
            else if (gotType)
            {
                if (itf != null && !theType.IsAssignableFrom(itf.GetType()))
                {
                    if (theType.IsInterface)
                    {
                        itf = InterfaceProjection(theType, itf);
                    }
                    else
                    {
                        try
                        {
                            object tResult;

                            tResult = InvocationBinding.Conversion(bindSite, theType, explict: true);

                            itf = tResult;
                        }
                        catch (RuntimeBinderException)
                        {
                            itf = MaybeConvert(theType, itf);
                        }
                    }
                }
                else if (null == itf && theType.IsValueType)
                {
                    itf = InvocationBinding.CreateInstance(theType);
                }
            }

            return(itf);
        }
Exemplo n.º 17
0
        internal static bool WireUpForInterface(this ShapeableObject bindSite, string binderName, bool found,
                                                ref object itf)
        {
            if (itf is InterceptorAddRemove)
            {
                return(true);
            }

            Type theType;
            bool gotType = bindSite.GetTypeForPropertyNameFromInterface(binderName, out theType);

            if (gotType && theType == typeof(void))
            {
                return(true);
            }

            if (found)
            {
                itf = WireUpForInterfaceUsingType(gotType, theType, bindSite, itf);
            }
            else
            {
                itf = null;
                if (!gotType)
                {
                    return(false);
                }

                if (theType.IsValueType)
                {
                    itf = InvocationBinding.CreateInstance(theType);
                }
            }

            return(true);
        }
 public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
 {
     result = InvocationBinding.CreateInstance(_type,
                                               TypeFactorization.MaybeRenameArguments(binder.CallInfo, args));
     return(true);
 }
Exemplo n.º 19
0
 public virtual TInterface DressedAs <TInterface>(params Type[] otherInterfaces) where TInterface : class
 {
     return(InvocationBinding.DressedAs <TInterface>(this, otherInterfaces));
 }
 protected virtual object CreateType(Type type, params object[] args)
 {
     return(InvocationBinding.CreateInstance(type, args));
 }
Exemplo n.º 21
0
        public virtual object Invoke(object target, params object[] args)
        {
            switch (Kind)
            {
            case InvocationKind.Constructor:
                return(InvocationBinding.CreateInstance((Type)target, args));

            case InvocationKind.Convert:
                bool tExplict = false;
                if (Arguments.Length == 2)
                {
                    tExplict = (bool)args[1];
                }
                return(InvocationBinding.Conversion(target, (Type)args[0], tExplict));

            case InvocationKind.Get:
                return(InvocationBinding.InvokeGet(target, Name.Name));

            case InvocationKind.Set:
                InvocationBinding.InvokeSet(target, Name.Name, args.FirstOrDefault());
                return(null);

            case InvocationKind.GetIndex:
                return(InvocationBinding.InvokeGetIndex(target, args));

            case InvocationKind.SetIndex:
                InvocationBinding.InvokeSetIndex(target, args);
                return(null);

            case InvocationKind.InvokeMember:
                return(InvocationBinding.InvokeMember(target, Name, args));

            case InvocationKind.InvokeMemberAction:
                InvocationBinding.InvokeMemberAction(target, Name, args);
                return(null);

            case InvocationKind.InvokeMemberUnknown:
            {
                try
                {
                    return(InvocationBinding.InvokeMember(target, Name, args));
                }
                catch (RuntimeBinderException)
                {
                    InvocationBinding.InvokeMemberAction(target, Name, args);
                    return(null);
                }
            }

            case InvocationKind.Invoke:
                return(InvocationBinding.Invoke(target, args));

            case InvocationKind.InvokeAction:
                InvocationBinding.InvokeAction(target, args);
                return(null);

            case InvocationKind.InvokeUnknown:
            {
                try
                {
                    return(InvocationBinding.Invoke(target, args));
                }
                catch (RuntimeBinderException)
                {
                    InvocationBinding.InvokeAction(target, args);
                    return(null);
                }
            }

            case InvocationKind.AddAssign:
                InvocationBinding.InvokeAddAssign(target, Name.Name, args.FirstOrDefault());
                return(null);

            case InvocationKind.SubtractAssign:
                InvocationBinding.InvokeSubtractAssign(target, Name.Name, args.FirstOrDefault());
                return(null);

            case InvocationKind.IsEvent:
                return(InvocationBinding.InvokeIsEvent(target, Name.Name));

            default:
                throw new InvalidOperationException("Unknown Invocation Kind: " + Kind);
            }
        }
Exemplo n.º 22
0
 public virtual dynamic Create()
 {
     object[] tArgs = Arguments();
     return(InvocationBinding.CreateInstance(Type, tArgs));
 }
Exemplo n.º 23
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);
        }
Exemplo n.º 24
0
        public override object Invoke(object target, params object[] args)
        {
            var invocationContext = target as InvocationContext;

            if (invocationContext != null)
            {
                target = invocationContext.Target;
            }

            if (args == null)
            {
                args = new object[] { null };
            }

            ValidateInvocationArguments(args);

            switch (Kind)
            {
            case InvocationKind.Constructor:
                var tTarget = (Type)target;
                return(InvocationMapping.InvokeConstructorCallSite(tTarget, tTarget.IsValueType, args,
                                                                   _argumentNames, _context,
                                                                   ref _callSite));

            case InvocationKind.Convert:
                return(InvocationMapping.InvokeConvertCallSite(target, _convertExplict, _convertType, _context,
                                                               ref _callSite));

            case InvocationKind.Get:
                return(InvocationMapping.InvokeGetCallSite(target, Name.Name, _context, _staticContext,
                                                           ref _callSite));

            case InvocationKind.Set:
                InvocationMapping.InvokeSetCallSite(target, Name.Name, args[0], _context, _staticContext,
                                                    ref _callSite);
                return(null);

            case InvocationKind.GetIndex:
                return(InvocationMapping.InvokeGetIndexCallSite(target, args, _argumentNames, _context,
                                                                _staticContext, ref _callSite));

            case InvocationKind.SetIndex:
                InvocationBinding.InvokeSetIndex(target, args);
                return(null);

            case InvocationKind.InvokeMember:
                return(InvocationMapping.InvokeMemberCallSite(target, Name, args, _argumentNames, _context,
                                                              _staticContext, ref _callSite));

            case InvocationKind.InvokeMemberAction:
                InvocationMapping.InvokeMemberActionCallSite(target, Name, args, _argumentNames, _context,
                                                             _staticContext, ref _callSite);
                return(null);

            case InvocationKind.InvokeMemberUnknown:
            {
                try
                {
                    var tObj = InvocationMapping.InvokeMemberCallSite(target, Name, args, _argumentNames,
                                                                      _context, _staticContext, ref _callSite);
                    return(tObj);
                }
                catch (RuntimeBinderException)
                {
                    InvocationMapping.InvokeMemberActionCallSite(target, Name, args, _argumentNames, _context,
                                                                 _staticContext, ref _callSite2);
                    return(null);
                }
            }

            case InvocationKind.Invoke:
                return(InvocationMapping.InvokeDirectCallSite(target, args, _argumentNames, _context, _staticContext,
                                                              ref _callSite));

            case InvocationKind.InvokeAction:
                InvocationMapping.InvokeDirectActionCallSite(target, args, _argumentNames, _context, _staticContext,
                                                             ref _callSite);
                return(null);

            case InvocationKind.InvokeUnknown:
            {
                try
                {
                    var tObj = InvocationMapping.InvokeDirectCallSite(target, args, _argumentNames, _context,
                                                                      _staticContext, ref _callSite);
                    return(tObj);
                }
                catch (RuntimeBinderException)
                {
                    InvocationMapping.InvokeDirectActionCallSite(target, args, _argumentNames, _context,
                                                                 _staticContext, ref _callSite2);
                    return(null);
                }
            }

            case InvocationKind.AddAssign:
                InvocationMapping.InvokeAddAssignCallSite(target, Name.Name, args, _argumentNames, _context,
                                                          _staticContext, ref _callSite, ref _callSite2,
                                                          ref _callSite3, ref _callSite4);
                return(null);

            case InvocationKind.SubtractAssign:
                InvocationMapping.InvokeSubtractAssignCallSite(target, Name.Name, args, _argumentNames, _context,
                                                               _staticContext, ref _callSite, ref _callSite2,
                                                               ref _callSite3, ref _callSite4);
                return(null);

            case InvocationKind.IsEvent:
                return(InvocationMapping.InvokeIsEventCallSite(target, Name.Name, _context, ref _callSite));

            default:
                throw new InvalidOperationException("Unknown Invocation Kind: " + Kind);
            }
        }