Esempio n. 1
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. 2
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);
        }
Esempio n. 3
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);
        }
 protected virtual object CreateType(Type type, params object[] args)
 {
     return(InvocationBinding.CreateInstance(type, args));
 }
Esempio n. 5
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);
            }
        }
Esempio n. 6
0
 public virtual dynamic Create()
 {
     object[] tArgs = Arguments();
     return(InvocationBinding.CreateInstance(Type, tArgs));
 }
 public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
 {
     result = InvocationBinding.CreateInstance(_type,
                                               TypeFactorization.MaybeRenameArguments(binder.CallInfo, args));
     return(true);
 }