Ejemplo n.º 1
0
 protected BinderHash(Type delegateType, InvokeMemberName name, Type context, string[] argNames, Type binderType, bool staticContext, bool isEvent, bool knownBinder)
 {
     KnownBinder = knownBinder;
     BinderType = binderType;
     StaticContext = staticContext;
     DelegateType = delegateType;
     Name = name.Name;
     IsSpecialName = name.IsSpecialName;
     GenericArgs = name.GenericArgs;
     Context = context;
     ArgNames = argNames;
     IsEvent = isEvent;
 }
Ejemplo n.º 2
0
            /// <summary>
            /// Tries the invoke.
            /// </summary>
            /// <param name="binder">The binder.</param>
            /// <param name="args">The args.</param>
            /// <param name="result">The result.</param>
            /// <returns></returns>
            public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
            {
                object[] tArgs = args;
                if (OverloadTypes.ContainsKey(args.Length))
                {
                    tArgs = OverloadTypes[args.Length].Zip(args, Tuple.Create)
                            .Select(it => it.Item2 != null ? Dynamic.InvokeConvert(it.Item2, it.Item1, @explicit: true) : null).ToArray();
                }

                var name = InvokeMemberName.Create(Name, GenericMethodParameters);

                result = Parent.InvokeStaticMethod(name, tArgs);
                return(true);
            }
Ejemplo n.º 3
0
        internal static CallSite CreateCallSite(
            Type delegateType,
            Type specificBinderType,
            int knownType,
            LazyBinder binder,
            InvokeMemberName name,
            Type context,
            string[] argNames  = null,
            bool staticContext = false,
            bool isEvent       = false

            )
        {
            CallSite <DynamicCreateCallSite> tSite;

            if (!_dynamicInvokeCreateCallSite.TryGetValue(delegateType, out tSite))
            {
                tSite = CallSite <DynamicCreateCallSite> .Create(
                    Binder.InvokeMember(
                        CSharpBinderFlags.None,
                        "CreateCallSite",
                        new[] { delegateType },
                        typeof(InvokeHelper),
                        new[]
                {
                    CSharpArgumentInfo.Create(
                        CSharpArgumentInfoFlags.IsStaticType | CSharpArgumentInfoFlags.UseCompileTimeType,
                        null),                                                                   // InvokeHelper
                    CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null), //binderType
                    CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null), //knownType
                    CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null), //binder
                    CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null), //name
                    CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null), //context
                    CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null), //argnames
                    CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null), //staticcontext
                    CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null), //isevent
                }
                        ));

                _dynamicInvokeCreateCallSite[delegateType] = tSite;
            }
            return((CallSite)tSite.Target(tSite, typeof(InvokeHelper), specificBinderType, knownType, binder, name, context, argNames, staticContext, isEvent));
        }
Ejemplo n.º 4
0
        private static dynamic GetContextFromProperty(Type type)
        {
            try
            {
                const string     propertyName = "get_Context";
                InvokeMemberName invokeArgs   = propertyName;
                if (type.IsGenericTypeDefinition)
                {
                    var typeArgs = Enumerable.Repeat(typeof(object), type.GetGenericArguments().Length);
                    type = type.MakeGenericType(typeArgs.ToArray());
                }

                return(Impromptu.InvokeGet(type.WithStaticContext(), "Context"));
            }
            catch (RuntimeBinderException)
            {
                return(null);
            }
        }
        private static dynamic GetContextFromProperty(Type type)
        {
            try
            {
                const string     propertyName = "get_Context";
                InvokeMemberName invokeArgs   = propertyName;
                if (type.GetTypeInfo().IsGenericTypeDefinition)
                {
                    var typeArgs = Enumerable.Repeat(typeof(object), type.GetGenericArguments().Length);
                    type = type.MakeGenericType(typeArgs.ToArray());
                }

                var staticContext = InvokeContext.CreateStatic;
                return(Dynamic.InvokeGet(staticContext(type), "Context"));
            }
            catch (RuntimeBinderException)
            {
                return(FallbackResolver.GetContext(type, null));
            }
        }
Ejemplo n.º 6
0
        internal static dynamic DynamicInvokeStaticMember(Type tReturn, ref CallSite callsite, Type binderType, int knownType, LazyBinder binder,
                                                          InvokeMemberName name,
                                                          bool staticContext,
                                                          Type context,
                                                          string[] argNames,
                                                          Type target, params object[] args)
        {
            CallSite <DynamicInvokeMemberConstructorValueType> tSite;

            if (!_dynamicInvokeMemberSite.TryGetValue(tReturn, out tSite))
            {
                tSite = CallSite <DynamicInvokeMemberConstructorValueType> .Create(
                    Binder.InvokeMember(
                        CSharpBinderFlags.None,
                        "InvokeMemberTargetType",
                        new[] { typeof(Type), tReturn },
                        typeof(InvokeHelper),
                        new[]
                {
                    CSharpArgumentInfo.Create(
                        CSharpArgumentInfoFlags.IsStaticType |
                        CSharpArgumentInfoFlags.UseCompileTimeType, null),
                    CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType | CSharpArgumentInfoFlags.IsRef, null),
                    CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null),
                    CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null),
                    CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null),
                    CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null),
                    CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null),
                    CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null),
                    CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null),
                    CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null),
                    CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null),
                }
                        )
                    );

                _dynamicInvokeMemberSite[tReturn] = tSite;
            }

            return(tSite.Target(tSite, typeof(InvokeHelper), ref callsite, binderType, knownType, binder, name, staticContext, context, argNames, target, args));
        }
Ejemplo n.º 7
0
        internal static object InvokeMemberCallSite(object target, InvokeMemberName name, object[] args, string[] tArgNames, Type tContext, bool tStaticContext, ref CallSite callSite)
        {
            LazyBinder tBinder     = null;
            Type       tBinderType = null;

            if (callSite == null)
            {
                tBinder = () =>
                {
                    var tList = GetBindingArgumentList(args, tArgNames, tStaticContext);
                    var tFlag = CSharpBinderFlags.None;
                    if (name.IsSpecialName)
                    {
                        tFlag |= CSharpBinderFlags.InvokeSpecialName;
                    }
                    return(Binder.InvokeMember(tFlag, name.Name, name.GenericArgs,
                                               tContext, tList));
                };
                tBinderType = typeof(InvokeMemberBinder);
            }


            return(InvokeMember <object>(ref callSite, tBinderType, KnownMember, tBinder, name, tStaticContext, tContext, tArgNames, target, args));
        }
Ejemplo n.º 8
0
        internal static TReturn InvokeMemberTargetType <TTarget, TReturn>(
            ref CallSite callsite,
            Type binderType,
            int knownType,
            LazyBinder binder,
            InvokeMemberName name,
            bool staticContext,
            Type context,
            string[] argNames,
            TTarget target, params object [] args)
        {
            var tSwitch = args.Length;

            switch (tSwitch)
            {
                #region Optimizations
            case 0:
            {
                var tCallSite = (CallSite <Func <CallSite, TTarget, TReturn> >)callsite;
                if (tCallSite == null)
                {
                    tCallSite = CreateCallSite <Func <CallSite, TTarget, TReturn> >(binderType, knownType, binder, name, context, argNames, staticContext);
                    callsite  = tCallSite;
                }
                return(tCallSite.Target(tCallSite, target));
            }

            case 1:
            {
                var tCallSite = (CallSite <Func <CallSite, TTarget, object, TReturn> >)callsite;
                if (tCallSite == null)
                {
                    tCallSite = CreateCallSite <Func <CallSite, TTarget, object, TReturn> >(binderType, knownType, binder, name, context, argNames, staticContext);
                    callsite  = tCallSite;
                }
                return(tCallSite.Target(tCallSite, target, args[0]));
            }

            case 2:
            {
                var tCallSite = (CallSite <Func <CallSite, TTarget, object, object, TReturn> >)callsite;
                if (tCallSite == null)
                {
                    tCallSite = CreateCallSite <Func <CallSite, TTarget, object, object, TReturn> >(binderType, knownType, binder, name, context, argNames, staticContext);
                    callsite  = tCallSite;
                }
                return(tCallSite.Target(tCallSite, target, args[0], args[1]));
            }

            case 3:
            {
                var tCallSite = (CallSite <Func <CallSite, TTarget, object, object, object, TReturn> >)callsite;
                if (tCallSite == null)
                {
                    tCallSite = CreateCallSite <Func <CallSite, TTarget, object, object, object, TReturn> >(binderType, knownType, binder, name, context, argNames, staticContext);
                    callsite  = tCallSite;
                }
                return(tCallSite.Target(tCallSite, target, args[0], args[1], args[2]));
            }

            case 4:
            {
                var tCallSite = (CallSite <Func <CallSite, TTarget, object, object, object, object, TReturn> >)callsite;
                if (tCallSite == null)
                {
                    tCallSite = CreateCallSite <Func <CallSite, TTarget, object, object, object, object, TReturn> >(binderType, knownType, binder, name, context, argNames, staticContext);
                    callsite  = tCallSite;
                }
                return(tCallSite.Target(tCallSite, target, args[0], args[1], args[2], args[3]));
            }

            case 5:
            {
                var tCallSite = (CallSite <Func <CallSite, TTarget, object, object, object, object, object, TReturn> >)callsite;
                if (tCallSite == null)
                {
                    tCallSite = CreateCallSite <Func <CallSite, TTarget, object, object, object, object, object, TReturn> >(binderType, knownType, binder, name, context, argNames, staticContext);
                    callsite  = tCallSite;
                }
                return(tCallSite.Target(tCallSite, target, args[0], args[1], args[2], args[3], args[4]));
            }

            case 6:
            {
                var tCallSite = (CallSite <Func <CallSite, TTarget, object, object, object, object, object, object, TReturn> >)callsite;
                if (tCallSite == null)
                {
                    tCallSite = CreateCallSite <Func <CallSite, TTarget, object, object, object, object, object, object, TReturn> >(binderType, knownType, binder, name, context, argNames, staticContext);
                    callsite  = tCallSite;
                }
                return(tCallSite.Target(tCallSite, target, args[0], args[1], args[2], args[3], args[4], args[5]));
            }

            case 7:
            {
                var tCallSite = (CallSite <Func <CallSite, TTarget, object, object, object, object, object, object, object, TReturn> >)callsite;
                if (tCallSite == null)
                {
                    tCallSite = CreateCallSite <Func <CallSite, TTarget, object, object, object, object, object, object, object, TReturn> >(binderType, knownType, binder, name, context, argNames, staticContext);
                    callsite  = tCallSite;
                }
                return(tCallSite.Target(tCallSite, target, args[0], args[1], args[2], args[3], args[4], args[5], args[6]));
            }

            case 8:
            {
                var tCallSite = (CallSite <Func <CallSite, TTarget, object, object, object, object, object, object, object, object, TReturn> >)callsite;
                if (tCallSite == null)
                {
                    tCallSite = CreateCallSite <Func <CallSite, TTarget, object, object, object, object, object, object, object, object, TReturn> >(binderType, knownType, binder, name, context, argNames, staticContext);
                    callsite  = tCallSite;
                }
                return(tCallSite.Target(tCallSite, target, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]));
            }

            case 9:
            {
                var tCallSite = (CallSite <Func <CallSite, TTarget, object, object, object, object, object, object, object, object, object, TReturn> >)callsite;
                if (tCallSite == null)
                {
                    tCallSite = CreateCallSite <Func <CallSite, TTarget, object, object, object, object, object, object, object, object, object, TReturn> >(binderType, knownType, binder, name, context, argNames, staticContext);
                    callsite  = tCallSite;
                }
                return(tCallSite.Target(tCallSite, target, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]));
            }

            case 10:
            {
                var tCallSite = (CallSite <Func <CallSite, TTarget, object, object, object, object, object, object, object, object, object, object, TReturn> >)callsite;
                if (tCallSite == null)
                {
                    tCallSite = CreateCallSite <Func <CallSite, TTarget, object, object, object, object, object, object, object, object, object, object, TReturn> >(binderType, knownType, binder, name, context, argNames, staticContext);
                    callsite  = tCallSite;
                }
                return(tCallSite.Target(tCallSite, target, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]));
            }

            case 11:
            {
                var tCallSite = (CallSite <Func <CallSite, TTarget, object, object, object, object, object, object, object, object, object, object, object, TReturn> >)callsite;
                if (tCallSite == null)
                {
                    tCallSite = CreateCallSite <Func <CallSite, TTarget, object, object, object, object, object, object, object, object, object, object, object, TReturn> >(binderType, knownType, binder, name, context, argNames, staticContext);
                    callsite  = tCallSite;
                }
                return(tCallSite.Target(tCallSite, target, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10]));
            }

            case 12:
            {
                var tCallSite = (CallSite <Func <CallSite, TTarget, object, object, object, object, object, object, object, object, object, object, object, object, TReturn> >)callsite;
                if (tCallSite == null)
                {
                    tCallSite = CreateCallSite <Func <CallSite, TTarget, object, object, object, object, object, object, object, object, object, object, object, object, TReturn> >(binderType, knownType, binder, name, context, argNames, staticContext);
                    callsite  = tCallSite;
                }
                return(tCallSite.Target(tCallSite, target, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11]));
            }

            case 13:
            {
                var tCallSite = (CallSite <Func <CallSite, TTarget, object, object, object, object, object, object, object, object, object, object, object, object, object, TReturn> >)callsite;
                if (tCallSite == null)
                {
                    tCallSite = CreateCallSite <Func <CallSite, TTarget, object, object, object, object, object, object, object, object, object, object, object, object, object, TReturn> >(binderType, knownType, binder, name, context, argNames, staticContext);
                    callsite  = tCallSite;
                }
                return(tCallSite.Target(tCallSite, target, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12]));
            }

            case 14:
            {
                var tCallSite = (CallSite <Func <CallSite, TTarget, object, object, object, object, object, object, object, object, object, object, object, object, object, object, TReturn> >)callsite;
                if (tCallSite == null)
                {
                    tCallSite = CreateCallSite <Func <CallSite, TTarget, object, object, object, object, object, object, object, object, object, object, object, object, object, object, TReturn> >(binderType, knownType, binder, name, context, argNames, staticContext);
                    callsite  = tCallSite;
                }
                return(tCallSite.Target(tCallSite, target, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13]));
            }

                #endregion
            default:
                var tArgTypes     = Enumerable.Repeat(typeof(object), tSwitch);
                var tDelagateType = EmitCallSiteFuncType(tArgTypes, typeof(TTarget));
                return(Dynamic.InvokeCallSite(CreateCallSite(tDelagateType, binderType, knownType, binder, name, context, argNames), target, args));
            }
        }