/// <summary> /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided <see cref="CallSiteBinder" />. /// </summary> /// <param name="binder">The runtime binder for the dynamic operation.</param> /// <param name="returnType">The result type of the dynamic expression.</param> /// <param name="arg0">The first argument to the dynamic operation.</param> /// <param name="arg1">The second argument to the dynamic operation.</param> /// <param name="arg2">The third argument to the dynamic operation.</param> /// <param name="arg3">The fourth argument to the dynamic operation.</param> /// <returns> /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the /// <see cref="DynamicExpression.Binder">Binder</see> and /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values. /// </returns> /// <remarks> /// The <see cref="DynamicExpression.DelegateType">DelegateType</see> property of the /// result will be inferred from the types of the arguments and the specified return type. /// </remarks> public static DynamicExpression Dynamic(CallSiteBinder binder, Type returnType, Expression arg0, Expression arg1, Expression arg2, Expression arg3) => DynamicExpression.Dynamic(binder, returnType, arg0, arg1, arg2, arg3);
/// <summary> /// Creates a CallSite with the given delegate type and binder. /// </summary> /// <param name="delegateType">The CallSite delegate type.</param> /// <param name="binder">The CallSite binder.</param> /// <returns>The new CallSite.</returns> public static CallSite Create(Type delegateType, CallSiteBinder binder) { ContractUtils.RequiresNotNull(delegateType, "delegateType"); ContractUtils.RequiresNotNull(binder, "binder"); ContractUtils.Requires(delegateType.IsSubclassOf(typeof(Delegate)), "delegateType", Strings.TypeMustBeDerivedFromSystemDelegate); if (_SiteCtors == null) { // It's okay to just set this, worst case we're just throwing away some data _SiteCtors = new CacheDict<Type, Func<CallSiteBinder, CallSite>>(100); } Func<CallSiteBinder, CallSite> ctor; MethodInfo method = null; var ctors = _SiteCtors; lock (ctors) { if (!ctors.TryGetValue(delegateType, out ctor)) { method = typeof(CallSite<>).MakeGenericType(delegateType).GetMethod("Create"); if (TypeUtils.CanCache(delegateType)) { ctor = (Func<CallSiteBinder, CallSite>)Delegate.CreateDelegate(typeof(Func<CallSiteBinder, CallSite>), method); ctors.Add(delegateType, ctor); } } } if (ctor != null) { return ctor(binder); } // slow path return (CallSite)method.Invoke(null, new object[] { binder }); }
public static Instruction Factory(CallSiteBinder binder) { return(new DynamicInstruction <TRet>(CallSite <Func <CallSite, TRet> > .Create(binder))); }
/// <summary> /// Gets or creates a dynamic site w/ the specified type parameters for the provided binder. /// </summary> /// <remarks> /// This will either get the site from the cache or create a new site and return it. The cache /// may be cleaned if it's gotten too big since the last usage. /// </remarks> public CallSite <Func <CallSite, T1, T2, T3, TResult> > GetOrCreateSite <T1, T2, T3, TResult>(CallSiteBinder siteBinder) { return(GetOrCreateSite <CallSite <Func <CallSite, T1, T2, T3, TResult> > >(siteBinder, CallSite <Func <CallSite, T1, T2, T3, TResult> > .Create)); }
internal override Expression GetExpression(List <ParameterExpression> parameters, Dictionary <string, ConstantExpression> locals, List <DataContainer> dataContainers, Type dynamicContext, LabelTarget label, bool requiresReturnValue = true) { CallSiteBinder binder = Binder.GetIndex(CSharpBinderFlags.None, dynamicContext ?? typeof(object), new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }); return(Expression.Dynamic(binder, typeof(object), new Expression[] { Target.GetExpression(parameters, locals, dataContainers, dynamicContext, label) }.Concat(Indices.Arguments.Select(token => token.GetExpression(parameters, locals, dataContainers, dynamicContext, label))))); }
/// <summary> /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided <see cref="CallSiteBinder" /> and four arguments. /// </summary> /// <param name="delegateType">The type of the delegate used by the <see cref="CallSite" />.</param> /// <param name="binder">The runtime binder for the dynamic operation.</param> /// <param name="arg0">The first argument to the dynamic operation.</param> /// <param name="arg1">The second argument to the dynamic operation.</param> /// <param name="arg2">The third argument to the dynamic operation.</param> /// <param name="arg3">The fourth argument to the dynamic operation.</param> /// <returns> /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the /// <see cref="DynamicExpression.DelegateType">DelegateType</see>, /// <see cref="DynamicExpression.Binder">Binder</see>, and /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values. /// </returns> public static DynamicExpression MakeDynamic(Type delegateType, CallSiteBinder binder, Expression arg0, Expression arg1, Expression arg2, Expression arg3) => DynamicExpression.MakeDynamic(delegateType, binder, arg0, arg1, arg2, arg3);
// only CallSite<T> derives from this internal CallSite(CallSiteBinder binder) { _binder = binder; }
public void EmitDynamic <T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TRet>(CallSiteBinder binder) { Emit(DynamicInstruction <T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TRet> .Factory(binder)); }
public void EmitDynamic(Type type, CallSiteBinder binder) { Emit(CreateDynamicInstruction(type, binder)); }
/// <summary> /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided /// <see cref="CallSiteBinder" />. /// </summary> /// <param name="delegateType">The type of the delegate used by the <see cref="CallSite" />.</param> /// <param name="binder">The runtime binder for the dynamic operation.</param> /// <param name="arguments">The arguments to the dynamic operation.</param> /// <returns> /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the /// <see cref="DynamicExpression.DelegateType">DelegateType</see>, /// <see cref="DynamicExpression.Binder">Binder</see>, and /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values. /// </returns> public static DynamicExpression MakeDynamic(Type delegateType, CallSiteBinder binder, params Expression[] arguments) { return(MakeDynamic(delegateType, binder, (IEnumerable <Expression>)arguments)); }
// Dev10 Bug 914027 - Changed the callsite's target parameter from dynamic to object, see comment at top for details public static CallSite <Func <CallSite, object, object> > GetMemberAccessCallSite(CallSiteBinder binder) { return(CallSite <Func <CallSite, object, object> > .Create(binder)); }
/// <summary> /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided /// <see cref="CallSiteBinder" /> and three arguments. /// </summary> /// <param name="delegateType">The type of the delegate used by the <see cref="CallSite" />.</param> /// <param name="binder">The runtime binder for the dynamic operation.</param> /// <param name="arg0">The first argument to the dynamic operation.</param> /// <param name="arg1">The second argument to the dynamic operation.</param> /// <param name="arg2">The third argument to the dynamic operation.</param> /// <returns> /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the /// <see cref="DynamicExpression.DelegateType">DelegateType</see>, /// <see cref="DynamicExpression.Binder">Binder</see>, and /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values. /// </returns> public static DynamicExpression MakeDynamic(Type delegateType, CallSiteBinder binder, Expression arg0, Expression arg1, Expression arg2) { return(DynamicExpression.MakeDynamic(delegateType, binder, arg0, arg1, arg2)); }
/// <summary> /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided /// <see cref="CallSiteBinder" />. /// </summary> /// <param name="binder">The runtime binder for the dynamic operation.</param> /// <param name="returnType">The result type of the dynamic expression.</param> /// <param name="arg0">The first argument to the dynamic operation.</param> /// <param name="arg1">The second argument to the dynamic operation.</param> /// <returns> /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the /// <see cref="DynamicExpression.Binder">Binder</see> and /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values. /// </returns> /// <remarks> /// The <see cref="DynamicExpression.DelegateType">DelegateType</see> property of the /// result will be inferred from the types of the arguments and the specified return type. /// </remarks> public static DynamicExpression Dynamic(CallSiteBinder binder, Type returnType, Expression arg0, Expression arg1) { return(DynamicExpression.Dynamic(binder, returnType, arg0, arg1)); }
public static Instruction Factory(CallSiteBinder binder) { return(new DynamicInstruction <T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TRet>(CallSite <Func <CallSite, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TRet> > .Create(binder))); }
/// <summary> /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided <see cref="CallSiteBinder" />. /// </summary> /// <param name="binder">The runtime binder for the dynamic operation.</param> /// <param name="returnType">The result type of the dynamic expression.</param> /// <param name="arguments">The arguments to the dynamic operation.</param> /// <returns> /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the /// <see cref="DynamicExpression.Binder">Binder</see> and /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values. /// </returns> /// <remarks> /// The <see cref="DynamicExpression.DelegateType">DelegateType</see> property of the /// result will be inferred from the types of the arguments and the specified return type. /// </remarks> public static DynamicExpression Dynamic(CallSiteBinder binder, Type returnType, params Expression[] arguments) => DynamicExpression.Dynamic(binder, returnType, arguments);
protected abstract void ReduceDynamic(out CallSiteBinder binder, out IEnumerable <Expression> arguments, out Type[] argumentTypes);
/// <summary> /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided <see cref="CallSiteBinder" />. /// </summary> /// <param name="delegateType">The type of the delegate used by the <see cref="CallSite" />.</param> /// <param name="binder">The runtime binder for the dynamic operation.</param> /// <param name="arguments">The arguments to the dynamic operation.</param> /// <returns> /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the /// <see cref="DynamicExpression.DelegateType">DelegateType</see>, /// <see cref="DynamicExpression.Binder">Binder</see>, and /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values. /// </returns> public static DynamicExpression MakeDynamic(Type delegateType, CallSiteBinder binder, IEnumerable <Expression>?arguments) => DynamicExpression.MakeDynamic(delegateType, binder, arguments);
internal InterceptorSiteBinder(CallSiteBinder binder) { _binder = binder; }
/// <summary> /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided <see cref="CallSiteBinder" />. /// </summary> /// <param name="delegateType">The type of the delegate used by the <see cref="CallSite" />.</param> /// <param name="binder">The runtime binder for the dynamic operation.</param> /// <param name="arguments">The arguments to the dynamic operation.</param> /// <returns> /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the /// <see cref="DynamicExpression.DelegateType">DelegateType</see>, /// <see cref="DynamicExpression.Binder">Binder</see>, and /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values. /// </returns> public static DynamicExpression MakeDynamic(Type delegateType, CallSiteBinder binder, params Expression[]?arguments) => MakeDynamic(delegateType, binder, (IEnumerable <Expression>?)arguments);
/// <summary> /// Reduces the dynamic expression to a binder and a set of arguments to apply the operation to. /// </summary> /// <param name="binder">The binder used to perform the dynamic operation.</param> /// <param name="arguments">The arguments to apply the dynamic operation to.</param> /// <param name="argumentTypes">The types of the arguments to use for the dynamic call site. Return null to infer types.</param> protected override void ReduceDynamic(out CallSiteBinder binder, out IEnumerable <Expression> arguments, out Type[] argumentTypes) => throw Unreachable;
/// <summary> /// Gets or creates a dynamic site w/ the specified type parameters for the provided binder. /// </summary> /// <remarks> /// This will either get the site from the cache or create a new site and return it. The cache /// may be cleaned if it's gotten too big since the last usage. /// </remarks> public CallSite <Action <CallSite, T1> > GetOrCreateActionSite <T1>(CallSiteBinder siteBinder) { return(GetOrCreateSite <CallSite <Action <CallSite, T1> > >(siteBinder, CallSite <Action <CallSite, T1> > .Create)); }
/// <summary> /// Reduces the dynamic expression to a binder and a set of arguments to apply the operation to. /// </summary> /// <param name="binder">The binder used to perform the dynamic operation.</param> /// <param name="arguments">The arguments to apply the dynamic operation to.</param> /// <param name="argumentTypes">The types of the arguments to use for the dynamic call site. Return null to infer types.</param> protected override void ReduceDynamic(out CallSiteBinder binder, out IEnumerable <Expression> arguments, out Type[] argumentTypes) { binder = Binder.Convert(Flags, Type, Context); arguments = new[] { Expression }; argumentTypes = null; }
/// <summary> /// Gets or creates a dynamic site w/ the specified type parameters for the provided binder. /// </summary> /// <remarks> /// This will either get the site from the cache or create a new site and return it. The cache /// may be cleaned if it's gotten too big since the last usage. /// </remarks> public CallSite <TSiteFunc> GetOrCreateSite <TSiteFunc>(CallSiteBinder siteBinder) where TSiteFunc : class { return(GetOrCreateSite <CallSite <TSiteFunc> >(siteBinder, CallSite <TSiteFunc> .Create)); }
// More proper would be to make this a virtual method on Action private static string FormatBinder(CallSiteBinder binder) { ConvertBinder convert; GetMemberBinder getMember; SetMemberBinder setMember; DeleteMemberBinder deleteMember; InvokeMemberBinder call; UnaryOperationBinder unary; BinaryOperationBinder binary; if ((convert = binder as ConvertBinder) != null) { return("Convert " + convert.Type); } else if ((getMember = binder as GetMemberBinder) != null) { return("GetMember " + getMember.Name); } else if ((setMember = binder as SetMemberBinder) != null) { return("SetMember " + setMember.Name); } else if ((deleteMember = binder as DeleteMemberBinder) != null) { return("DeleteMember " + deleteMember.Name); } else if (binder is GetIndexBinder) { return("GetIndex"); } else if (binder is SetIndexBinder) { return("SetIndex"); } else if (binder is DeleteIndexBinder) { return("DeleteIndex"); } else if ((call = binder as InvokeMemberBinder) != null) { return("Call " + call.Name); } else if (binder is InvokeBinder) { return("Invoke"); } else if (binder is CreateInstanceBinder) { return("Create"); } else if ((unary = binder as UnaryOperationBinder) != null) { return(unary.Operation.ToString()); } else if ((binary = binder as BinaryOperationBinder) != null) { return(binary.Operation.ToString()); } else { return("CallSiteBinder"); } }
public void EmitDynamic <T0, T1, T2, T3, T4, TRet>(CallSiteBinder binder) { Emit(DynamicInstruction <T0, T1, T2, T3, T4, TRet> .Factory(binder)); }
/// <summary> /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided <see cref="CallSiteBinder" />. /// </summary> /// <param name="binder">The runtime binder for the dynamic operation.</param> /// <param name="returnType">The result type of the dynamic expression.</param> /// <param name="arguments">The arguments to the dynamic operation.</param> /// <returns> /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the /// <see cref="DynamicExpression.Binder">Binder</see> and /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values. /// </returns> /// <remarks> /// The <see cref="DynamicExpression.DelegateType">DelegateType</see> property of the /// result will be inferred from the types of the arguments and the specified return type. /// </remarks> public static DynamicExpression Dynamic(CallSiteBinder binder, Type returnType, IEnumerable <Expression> arguments) => DynamicExpression.Dynamic(binder, returnType, arguments);
// More proper would be to make this a virtual method on Action private static string FormatBinder(CallSiteBinder binder) { ConvertBinder convert; GetMemberBinder getMember; SetMemberBinder setMember; DeleteMemberBinder deleteMember; InvokeMemberBinder call; UnaryOperationBinder unary; BinaryOperationBinder binary; #pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. if ((convert = binder as ConvertBinder) != null) { return("Convert " + convert.Type); } else if ((getMember = binder as GetMemberBinder) != null) { return("GetMember " + getMember.Name); } else if ((setMember = binder as SetMemberBinder) != null) { return("SetMember " + setMember.Name); } else if ((deleteMember = binder as DeleteMemberBinder) != null) { return("DeleteMember " + deleteMember.Name); } else if (binder is GetIndexBinder) { return("GetIndex"); } else if (binder is SetIndexBinder) { return("SetIndex"); } else if (binder is DeleteIndexBinder) { return("DeleteIndex"); } else if ((call = binder as InvokeMemberBinder) != null) { return("Call " + call.Name); } else if (binder is InvokeBinder) { return("Invoke"); } else if (binder is CreateInstanceBinder) { return("Create"); } else if ((unary = binder as UnaryOperationBinder) != null) { return(unary.Operation.ToString()); } else if ((binary = binder as BinaryOperationBinder) != null) { return(binary.Operation.ToString()); } else { return("CallSiteBinder"); } }