Exemplo n.º 1
0
        /// <summary>
        ///   Provides the implementation for operations that invoke a member. Classes derived from the <see
        ///    cref="T:System.Dynamic.DynamicObject" /> class can override this method to specify dynamic behavior for operations such as calling a method.
        /// </summary>
        /// <param name="binder"> Provides information about the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the statement sampleObject.SampleMethod(100), where sampleObject is an instance of the class derived from the <see
        ///    cref="T:System.Dynamic.DynamicObject" /> class, binder.Name returns "SampleMethod". The binder.IgnoreCase property specifies whether the member name is case-sensitive. </param>
        /// <param name="args"> The arguments that are passed to the object member during the invoke operation. For example, for the statement sampleObject.SampleMethod(100), where sampleObject is derived from the <see
        ///    cref="T:System.Dynamic.DynamicObject" /> class, <paramref name="args" /> is equal to 100. </param>
        /// <param name="result"> The result of the member invocation. </param>
        /// <returns> true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a language-specific run-time exception is thrown.) </returns>
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            if (!base.TryInvokeMember(binder, args, out result))
            {
                result = Impromptu.InvokeGet(CallTarget, binder.Name);
                if (result == null)
                {
                    return(false);
                }
                var tDel = result as Delegate;
                if (!binder.CallInfo.ArgumentNames.Any() && tDel != null)
                {
                    try {
                        result = this.InvokeMethodDelegate(tDel, args);
                    } catch (RuntimeBinderException)
                    //If it has out parmaters etc it can't be invoked dynamically like this.
                    //if we return false it will be handle by the GetProperty and then handled by the original dynamic invocation
                    {
                        return(false);
                    }
                }
                try {
                    result = Impromptu.Invoke(result, Util.NameArgsIfNecessary(binder.CallInfo, args));
                } catch (RuntimeBinderException) //If it has out parmaters etc it can't be invoked dynamically like this.
                //if we return false it will be handle by the GetProperty and then handled by the original dynamic invocation
                {
                    return(false);
                }
            }

            return(this.MassageResultBasedOnInterface(binder.Name, true, ref result));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Provides the implementation for operations that invoke an object. Classes derived from the <see cref="T:System.Dynamic.DynamicObject"/> class can override this method to specify dynamic behavior for operations such as invoking an object or a delegate.
        /// </summary>
        /// <param name="binder">Provides information about the invoke operation.</param>
        /// <param name="args">
        /// The arguments that are passed to the object during the invoke operation.
        /// For example, for the sampleObject(100) operation, where sampleObject is derived
        /// from the <see cref="T:System.Dynamic.DynamicObject"/> class, <paramref name="args"/>[0] is equal to 100.
        /// </param>
        /// <param name="result">The result of the object invocation.</param>
        /// <returns>
        /// true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a language-specific run-time exception is thrown.
        /// </returns>
        public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
        {
            if (CallTarget == null)
            {
                result = null;
                return(false);
            }

            var tArgs = Util.NameArgsIfNecessary(binder.CallInfo, args);

            try
            {
                result = Impromptu.Invoke(CallTarget, tArgs);
            }
            catch (RuntimeBinderException)
            {
                result = null;
                try
                {
                    Impromptu.InvokeAction(CallTarget, tArgs);
                }
                catch (RuntimeBinderException)
                {
                    return(false);
                }
            }
            return(true);
        }
Exemplo n.º 3
0
        internal static Delegate WrapFuncHelper <TReturn>(dynamic invokable, int length)
        {
            switch (length)
            {
            case 0:
                return(new Func <TReturn>(() => invokable()));

            case 1:
                return(new Func <object, TReturn>((a1) => invokable(a1)));

            case 2:
                return(new Func <object, object, TReturn>((a1, a2) => invokable(a1, a2)));

            case 3:
                return(new Func <object, object, object, TReturn>((a1, a2, a3) => invokable(a1, a2, a3)));

            case 4:
                return(new Func <object, object, object, object, TReturn>((a1, a2, a3, a4) => invokable(a1, a2, a3, a4)));

            case 5:
                return(new Func <object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5) => invokable(a1, a2, a3, a4, a5)));

            case 6:
                return(new Func <object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6) => invokable(a1, a2, a3, a4, a5, a6)));

            case 7:
                return(new Func <object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7) => invokable(a1, a2, a3, a4, a5, a6, a7)));

            case 8:
                return(new Func <object, object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7, a8) => invokable(a1, a2, a3, a4, a5, a6, a7, a8)));

            case 9:
                return(new Func <object, object, object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7, a8, a9) => invokable(a1, a2, a3, a4, a5, a6, a7, a8, a9)));

            case 10:
                return(new Func <object, object, object, object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) => invokable(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)));

            case 11:
                return(new Func <object, object, object, object, object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) => invokable(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11)));

            case 12:
                return(new Func <object, object, object, object, object, object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) => invokable(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12)));

            case 13:
                return(new Func <object, object, object, object, object, object, object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) => invokable(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13)));

            case 14:
                return(new Func <object, object, object, object, object, object, object, object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) => invokable(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14)));

            case 15:
                return(new Func <object, object, object, object, object, object, object, object, object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) => invokable(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15)));

            case 16:
                return(new Func <object, object, object, object, object, object, object, object, object, object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16) => invokable(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16)));

            default:
                return(new DynamicFunc <TReturn>(args => (TReturn)Impromptu.Invoke((object)invokable, args)));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Invokes the invocation on specified target with specific args.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="args">The args.</param>
        /// <returns></returns>
        public virtual object Invoke(object target, params object[] args)
        {
            switch (Kind)
            {
            case InvocationKind.Constructor:
                return(Impromptu.InvokeConstructor((Type)target, args));

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

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

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

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

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

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

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

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

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

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

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

            case InvocationKind.AddAssign:
                Impromptu.InvokeAddAssignMember(target, Name.Name, args.FirstOrDefault());
                return(null);

            case InvocationKind.SubtractAssign:
                Impromptu.InvokeSubtractAssignMember(target, Name.Name, args.FirstOrDefault());
                return(null);

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

            default:
                throw new InvalidOperationException("Unknown Invocation Kind: " + Kind);
            }
        }
Exemplo n.º 5
0
        internal static Delegate WrapFuncHelperMono <TReturn>(dynamic invokable, int length)
        {
            switch (length)
            {
                #region Optimizations
            case 0:
                return(new Func <TReturn>(() => {
                    object tResult = invokable();
                    return (TReturn)InvokeConvertCallSite(tResult, true, typeof(TReturn), typeof(object), ref MonoConvertCallSite <TReturn> .CallSite);
                }));

            case 1:
                return(new Func <object, TReturn>((a1) => {
                    object tResult = invokable(a1);
                    return (TReturn)InvokeConvertCallSite(tResult, true, typeof(TReturn), typeof(object), ref MonoConvertCallSite <TReturn> .CallSite);
                }));

            case 2:
                return(new Func <object, object, TReturn>((a1, a2) => {
                    object tResult = invokable(a1, a2);
                    return (TReturn)InvokeConvertCallSite(tResult, true, typeof(TReturn), typeof(object), ref MonoConvertCallSite <TReturn> .CallSite);
                }));

            case 3:
                return(new Func <object, object, object, TReturn>((a1, a2, a3) => {
                    object tResult = invokable(a1, a2, a3);
                    return (TReturn)InvokeConvertCallSite(tResult, true, typeof(TReturn), typeof(object), ref MonoConvertCallSite <TReturn> .CallSite);
                }));

            case 4:
                return(new Func <object, object, object, object, TReturn>((a1, a2, a3, a4) => {
                    object tResult = invokable(a1, a2, a3, a4);
                    return (TReturn)InvokeConvertCallSite(tResult, true, typeof(TReturn), typeof(object), ref MonoConvertCallSite <TReturn> .CallSite);
                }));

            case 5:
                return(new Func <object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5) => {
                    object tResult = invokable(a1, a2, a3, a4, a5);
                    return (TReturn)InvokeConvertCallSite(tResult, true, typeof(TReturn), typeof(object), ref MonoConvertCallSite <TReturn> .CallSite);
                }));

            case 6:
                return(new Func <object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6) => {
                    object tResult = invokable(a1, a2, a3, a4, a5, a6);
                    return (TReturn)InvokeConvertCallSite(tResult, true, typeof(TReturn), typeof(object), ref MonoConvertCallSite <TReturn> .CallSite);
                }));

            case 7:
                return(new Func <object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7) => {
                    object tResult = invokable(a1, a2, a3, a4, a5, a6, a7);
                    return (TReturn)InvokeConvertCallSite(tResult, true, typeof(TReturn), typeof(object), ref MonoConvertCallSite <TReturn> .CallSite);
                }));

            case 8:
                return(new Func <object, object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7, a8) => {
                    object tResult = invokable(a1, a2, a3, a4, a5, a6, a7, a8);
                    return (TReturn)InvokeConvertCallSite(tResult, true, typeof(TReturn), typeof(object), ref MonoConvertCallSite <TReturn> .CallSite);
                }));

            case 9:
                return(new Func <object, object, object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7, a8, a9) => {
                    object tResult = invokable(a1, a2, a3, a4, a5, a6, a7, a8, a9);
                    return (TReturn)InvokeConvertCallSite(tResult, true, typeof(TReturn), typeof(object), ref MonoConvertCallSite <TReturn> .CallSite);
                }));

            case 10:
                return(new Func <object, object, object, object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) => {
                    object tResult = invokable(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
                    return (TReturn)InvokeConvertCallSite(tResult, true, typeof(TReturn), typeof(object), ref MonoConvertCallSite <TReturn> .CallSite);
                }));

            case 11:
                return(new Func <object, object, object, object, object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) => {
                    object tResult = invokable(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11);
                    return (TReturn)InvokeConvertCallSite(tResult, true, typeof(TReturn), typeof(object), ref MonoConvertCallSite <TReturn> .CallSite);
                }));

            case 12:
                return(new Func <object, object, object, object, object, object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) => {
                    object tResult = invokable(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12);
                    return (TReturn)InvokeConvertCallSite(tResult, true, typeof(TReturn), typeof(object), ref MonoConvertCallSite <TReturn> .CallSite);
                }));

            case 13:
                return(new Func <object, object, object, object, object, object, object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) => {
                    object tResult = invokable(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13);
                    return (TReturn)InvokeConvertCallSite(tResult, true, typeof(TReturn), typeof(object), ref MonoConvertCallSite <TReturn> .CallSite);
                }));

            case 14:
                return(new Func <object, object, object, object, object, object, object, object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) => {
                    object tResult = invokable(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14);
                    return (TReturn)InvokeConvertCallSite(tResult, true, typeof(TReturn), typeof(object), ref MonoConvertCallSite <TReturn> .CallSite);
                }));

            case 15:
                return(new Func <object, object, object, object, object, object, object, object, object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) => {
                    object tResult = invokable(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15);
                    return (TReturn)InvokeConvertCallSite(tResult, true, typeof(TReturn), typeof(object), ref MonoConvertCallSite <TReturn> .CallSite);
                }));

            case 16:
                return(new Func <object, object, object, object, object, object, object, object, object, object, object, object, object, object, object, object, TReturn>((a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16) => {
                    object tResult = invokable(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16);
                    return (TReturn)InvokeConvertCallSite(tResult, true, typeof(TReturn), typeof(object), ref MonoConvertCallSite <TReturn> .CallSite);
                }));

                #endregion
            default:
                return(new DynamicFunc <TReturn>(args => {
                    object tResult = Impromptu.Invoke((object)invokable, args);
                    return (TReturn)InvokeConvertCallSite(tResult, true, typeof(TReturn), typeof(object), ref MonoConvertCallSite <TReturn> .CallSite);
                }));
            }
        }