Esempio n. 1
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. 2
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);
            }
        }