public static Action <TObject, TProperty> Set <TObject, TProperty>(string memberName, object context = null) { var callSite = CallSite <Action <CallSite, TObject, TProperty> > .Create( Binder.SetMember(CSharpBinderFlags.ResultDiscarded, memberName, Context(context), new[] { Arg, Arg })); return((obj, value) => callSite.Target(callSite, obj, value)); }
private static PropertyReader[] GetDynamicProperties(IDynamicMetaObjectProvider provider) { var metaObject = provider.GetMetaObject(Expression.Constant(provider)); var memberNames = metaObject.GetDynamicMemberNames(); // may return property names as well as method names, etc. var result = new List <PropertyReader>(); foreach (var name in memberNames) { try { var argumentInfo = new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }; var binder = Binder.GetMember(CSharpBinderFlags.None, name, provider.GetType(), argumentInfo); var site = CallSite <Func <CallSite, object, object> > .Create(binder); var value = site.Target(site, provider); // will throw if no valid property getter result.Add(new PropertyReader { Name = name, DeclaringType = provider.GetType(), Read = o => value }); } catch (RuntimeBinderException) { } } return(result.ToArray()); }
private static void AttemptImplicitCast <TFrom, TTo>() { // based on the IL produced by: // dynamic list = new List<TTo>(); // list.Add(Get<TFrom>()); // We can't use the above code because it will mimic a cast in a generic method // which doesn't have the same semantics as a cast in a non-generic method var list = new List <TTo>(capacity: 0); var binder = CSharpBinder.InvokeMember( flags: CSharpBinderFlags.ResultDiscarded, name: "Add", typeArguments: null, context: typeof(ConversionHelper), argumentInfo: new[] { CSharpArgumentInfo.Create(flags: CSharpArgumentInfoFlags.None, name: null), CSharpArgumentInfo.Create( flags: CSharpArgumentInfoFlags.UseCompileTimeType, name: null ), } ); var callSite = CallSite <Action <CallSite, object, TFrom> > .Create(binder); callSite.Target.Invoke(callSite, list, default(TFrom)); }
internal static Delegate WrapFunc(Type returnType, object functor, int length) { CallSite <DynamicInvokeWrapFunc> theCallSite; if (!_dynamicInvokeWrapFunc.TryGetValue(returnType, out theCallSite)) { var methodName = "WrapFunctionToDelegateMono"; #if !__MonoCS__ if (!TypeFactorization.IsMonoRuntimeEnvironment) { methodName = "WrapFunctionToDelegate"; } #endif theCallSite = CallSite <DynamicInvokeWrapFunc> .Create( Binder.InvokeMember( CSharpBinderFlags.None, methodName, new[] { returnType }, typeof(InvocationMapping), new[] { CSharpArgumentInfo.Create( CSharpArgumentInfoFlags.IsStaticType | CSharpArgumentInfoFlags.UseCompileTimeType, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null), })); _dynamicInvokeWrapFunc[returnType] = theCallSite; } return((Delegate)theCallSite.Target(theCallSite, typeof(InvocationMapping), functor, length)); }
public TypeWrapper(Type type) { foreach (PropertyInfo p in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { string name = p.Name; CallSite <Action <CallSite, object, object> > set = CallSite <Action <CallSite, object, object> > .Create( Binder.SetMember( CSharpBinderFlags.None, name, type, new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) })); _setters.Add(name, set); CallSite <Func <CallSite, object, object> > get = CallSite <Func <CallSite, object, object> > .Create( Binder.GetMember( CSharpBinderFlags.None, name, type, new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) })); _getters.Add(name, get); } }
internal static object InvokeConvertCallSite(object target, bool explict, Type type, Type context, ref CallSite callSite) { if (callSite == null) { LazyBinder binder = () => { var flags = explict ? CSharpBinderFlags.ConvertExplicit : CSharpBinderFlags.None; return(Binder.Convert(flags, type, context)); }; Type binderType = typeof(ConvertBinder); var func = BuildProxy.GenerateCallSiteFuncType(new Type[] {}, type); callSite = CreateCallSite(func, binderType, binder, explict ? Invocation.ExplicitConvertBinderName : Invocation.ImplicitConvertBinderName, context); } dynamic theDynamicCallSite = callSite; return(theDynamicCallSite.OriginalTarget(callSite, target)); }
internal static object InvokeConstructorCallSite(Type type, bool isValueType, object[] arguments, string[] argumentNames, Type context, ref CallSite callSite) { LazyBinder binder = null; Type binderType = typeof(InvokeConstructorDummy); if (callSite == null || isValueType) { if (isValueType && arguments.Length == 0) //dynamic invocation doesn't see no argument constructors of value types { return(Activator.CreateInstance(type)); } binder = () => { var tList = GetBindingArgumentList(arguments, argumentNames, true); return(Binder.InvokeConstructor(CSharpBinderFlags.None, type, tList)); }; } if (isValueType || TypeFactorization.IsMonoRuntimeEnvironment) { CallSite dummyCallSite = null; return(DynamicInvokeStaticMember(type, ref dummyCallSite, binderType, binder, Invocation.ConstructorBinderName, true, type, argumentNames, type, arguments)); } return(MemberTargetTypeInvoke <Type, object>(ref callSite, binderType, binder, Invocation.ConstructorBinderName, true, type, argumentNames, type, arguments)); }
internal static void InvokeMemberActionCallSite(object target, MemberInvocationMoniker name, object[] arguments, string[] argumentNames, Type context, bool staticContext, ref CallSite callSite) { LazyBinder binder = null; Type binderType = null; if (callSite == null) { binder = () => { var argumentBindingList = GetBindingArgumentList(arguments, argumentNames, staticContext); var flag = CSharpBinderFlags.ResultDiscarded; if (name.IsNameSpecial) { flag |= CSharpBinderFlags.InvokeSpecialName; } return(Binder.InvokeMember(flag, name.Name, name.GenericArguments, context, argumentBindingList)); }; binderType = typeof(InvokeMemberBinder); } MemberActionInvoke(ref callSite, binderType, binder, name, staticContext, context, argumentNames, target, arguments); }
internal static Delegate WrapFunc(Type returnType, object invokable, int length) { CallSite <DynamicInvokeWrapFunc> tSite; if (!_dynamicInvokeWrapFunc.TryGetValue(returnType, out tSite)) { var tMethod = "WrapFuncHelperMono"; #if !__MonoCS__ //Mono Compiler can't compile or run WrapFuncHelper if (!Util.IsMono) { tMethod = "WrapFuncHelper"; } #endif tSite = CallSite <DynamicInvokeWrapFunc> .Create( Binder.InvokeMember( CSharpBinderFlags.None, tMethod, new[] { returnType }, typeof(InvokeHelper), new[] { CSharpArgumentInfo.Create( CSharpArgumentInfoFlags.IsStaticType | CSharpArgumentInfoFlags.UseCompileTimeType, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null), } )); _dynamicInvokeWrapFunc[returnType] = tSite; } return((Delegate)tSite.Target(tSite, typeof(InvokeHelper), invokable, length)); }
private static object GetDynamically(MemberInfo member, object target) { var binder = Binder.GetMember(CSharpBinderFlags.None, member.Name, member.GetMemberType(), new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }); var callsite = CallSite<Func<CallSite, object, object>>.Create(binder); return callsite.Target(callsite, target); }
internal static object InvokeConstructorCallSite(Type type, bool isValueType, object[] args, string[] argNames, ref CallSite callSite) { LazyBinder tBinder = null; Type tBinderType = typeof(InvokeConstructorDummy); if (callSite == null || isValueType) { if (isValueType && args.Length == 0) //dynamic invocation doesn't see no argument constructors of value types { return(Activator.CreateInstance(type)); } tBinder = () => { var tList = GetBindingArgumentList(args, argNames, true); return(Binder.InvokeConstructor(CSharpBinderFlags.None, type, tList)); }; } if (isValueType || Util.IsMono) { CallSite tDummy = null; return(DynamicInvokeStaticMember(type, ref tDummy, tBinderType, KnownConstructor, tBinder, ConstructorName, true, type, argNames, type, args)); } return(InvokeMemberTargetType <Type, object>(ref callSite, tBinderType, KnownConstructor, tBinder, ConstructorName, true, type, argNames, type, args)); }
internal override Expression GetExpression(List <ParameterExpression> parameters, Dictionary <string, ConstantExpression> locals, List <DataContainer> dataContainers, Type dynamicContext, LabelTarget label, bool requiresReturnValue = true) { ParameterInfo[] pars = Method.GetParameters(); Expression[] args = new Expression[pars.Length]; for (int i = 0; i < pars.Length; ++i) { if (i < Arguments.Arguments.Length) { CallSiteBinder binder = Binder.Convert(CSharpBinderFlags.None, pars[i].ParameterType, dynamicContext ?? typeof(object)); args[i] = Expression.Dynamic(binder, pars[i].ParameterType, Arguments.Arguments[i].GetExpression(parameters, locals, dataContainers, dynamicContext, label)); } else { CallSiteBinder binder = Binder.Convert(CSharpBinderFlags.None, pars[i].ParameterType, dynamicContext ?? typeof(object)); args[i] = Expression.Dynamic(binder, pars[i].ParameterType, Expression.Constant(pars[i].DefaultValue, typeof(object))); } } var exp = Expression.Call(Method, args); if (requiresReturnValue) { return(Expression.Convert(exp, typeof(object))); } return(exp); }
internal static Delegate WrapFunc(Type returnType, object invokable, int length) { if (!_dynamicInvokeWrapFunc.TryGetValue(returnType, out var tSite)) { var tMethod = "WrapFuncHelper"; tSite = CallSite <DynamicInvokeWrapFunc> .Create( Binder.InvokeMember( CSharpBinderFlags.None, tMethod, new[] { returnType }, typeof(InvokeHelper), new[] { CSharpArgumentInfo.Create( CSharpArgumentInfoFlags.IsStaticType | CSharpArgumentInfoFlags.UseCompileTimeType, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null), } )); _dynamicInvokeWrapFunc[returnType] = tSite; } return((Delegate)tSite.Target(tSite, typeof(InvokeHelper), invokable, length)); }
private static object GetDynamicMember(object obj, string memberName) { var binder = Binder.GetMember(CSharpBinderFlags.None, memberName, null, new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }); var callsite = CallSite <Func <CallSite, object, object> > .Create(binder); return(callsite.Target(callsite, obj)); }
/// <summary> /// Provides implementation for binary operations. Classes derived from the <see cref="T:System.Dynamic.DynamicObject"/> class can override this method to specify dynamic behavior for operations such as addition and multiplication. /// </summary> /// <returns><c>true</c> if the operation is successful; otherwise, <c>false</c>. If this method returns <c>false</c>, the run-time binder of the language determines the behavior. (In most cases, a language-specific run-time exception is thrown.)</returns> /// <param name="binder">Provides information about the binary operation. The binder.Operation property returns an <see cref="T:System.Linq.Expressions.ExpressionType"/> object. For example, for the sum = first + second statement, where first and second are derived from the DynamicObject class, binder.Operation returns ExpressionType.Add.</param><param name="arg">The right operand for the binary operation. For example, for the sum = first + second statement, where first and second are derived from the DynamicObject class, <paramref name="arg"/> is equal to second.</param><param name="result">The result of the binary operation.</param> public override bool TryBinaryOperation(BinaryOperationBinder binder, object arg, out object result) { object resultOfCast; result = null; if (binder.Operation != ExpressionType.Equal) { return(false); } var convert = Binder.Convert(CSharpBinderFlags.None, arg.GetType(), typeof(ElasticsearchDynamicValue)); if (!TryConvert((ConvertBinder)convert, out resultOfCast)) { return(false); } result = (resultOfCast == null) ? Equals(arg, resultOfCast) : resultOfCast.Equals(arg); return(true); }
internal override Expression GetExpression(List <ParameterExpression> parameters, Dictionary <string, ConstantExpression> locals, List <DataContainer> dataContainers, Type dynamicContext, LabelTarget label) { CallSiteBinder binder = Binder.InvokeMember(CSharpBinderFlags.None, MethodName, Types, dynamicContext ?? typeof(object), new object[Arguments.Arguments.Length + 1].Select(val => CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null))); Expression dynamicCall = Expression.Dynamic(binder, typeof(object), new[] { Target.GetExpression(parameters, locals, dataContainers, dynamicContext, label) }.Concat(Arguments.Arguments.Select(token => token.GetExpression(parameters, locals, dataContainers, dynamicContext, label)))); var targetVar = Expression.Variable(typeof(object)); var argsVar = Expression.Variable(typeof(object[])); var methodVar = Expression.Variable(typeof(Tuple <MethodInfo, object[]>)); var resultVar = Expression.Variable(typeof(object)); Expression test = Expression.Equal(methodVar, Expression.Constant(null, typeof(Tuple <MethodInfo, object[]>))); Expression ifNotNull = Expression.Assign(resultVar, Expression.Call(Expression.Property(methodVar, Item1Prop), InvokeMethod, Expression.Constant(null), Expression.Property(methodVar, Item2Prop))); Expression ifNull = Expression.Assign(resultVar, dynamicCall); Expression branch = Expression.IfThenElse(test, ifNull, ifNotNull); Expression block = Expression.Block(new[] { targetVar, argsVar, methodVar }, new[] { Expression.Assign(targetVar, Target.GetExpression(parameters, locals, dataContainers, dynamicContext, label)), Expression.Assign(argsVar, Expression.NewArrayInit(typeof(object), new[] { targetVar }.Concat(Arguments.Arguments.Select(token => token.GetExpression(parameters, locals, dataContainers, dynamicContext, label))))), Expression.Assign(methodVar, Expression.Call(GetMethod, Expression.Constant(MethodName, typeof(string)), Expression.Constant(Types, typeof(Type[])), argsVar)), branch, resultVar }); Expression ret = Expression.Block(new[] { resultVar }, new Expression[] { Expression.IfThenElse(Expression.Property(null, SupportsExtensions), block, ifNull), resultVar }); return(ret); }
internal static void InvokeMemberActionCallSite(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 = () => { IEnumerable <CSharpArgumentInfo> tList; tList = GetBindingArgumentList(args, tArgNames, tStaticContext); var tFlag = CSharpBinderFlags.ResultDiscarded; if (name.IsSpecialName) { tFlag |= CSharpBinderFlags.InvokeSpecialName; } return(Binder.InvokeMember(tFlag, name.Name, name.GenericArgs, tContext, tList)); }; tBinderType = typeof(InvokeMemberBinder); } InvokeMemberAction(ref callSite, tBinderType, KnownMember, tBinder, name, tStaticContext, tContext, tArgNames, target, args); }
public void Set(object target, object value) { switch (target) { case IDictionary d: d[name] = value; break; case IDictionary <string, object> d: d[name] = value; break; default: var site = SetMemberSites.GetOrAdd( System.Tuple.Create(target.GetType(), name), t => CallSite <Func <CallSite, object, object, object> > .Create( Binder.GetMember( CSharpBinderFlags.None, t.Item2, t.Item1, new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }))); site.Target(site, target, value); break; } }
public static Func <TObject, TProperty> Get <TObject, TProperty>(string memberName, object context = null) { var callSite = CallSite <Func <CallSite, TObject, object> > .Create( Binder.GetMember(CSharpBinderFlags.None, memberName, Context(context), new[] { Arg })); return(obj => (TProperty)callSite.Target(callSite, obj)); }
private static CallSiteBinder get_binder() { return(Binder.InvokeMember(CSharpBinderFlags.None, "Power", null, MethodBase.GetCurrentMethod().DeclaringType, new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.Constant | CSharpArgumentInfoFlags.UseCompileTimeType, null) })); }
private static void SetObjectProperty(object target, string propertyName, object propertyValue) { var targetContext = GetTargetContext(target); var callSiteBinder = Binder.SetMember(CSharpBinderFlags.None, propertyName, targetContext, SetPropertyArgumentInfo); var callSite = CallSite <Func <CallSite, object, object, object> > .Create(callSiteBinder); callSite.Target(callSite, target, propertyValue); }
protected object GetDynamically(MemberInfo member, object target) { var binder = Binder.GetMember(CSharpBinderFlags.None, member.Name, ReflectionHelper.GetMemberType(member), new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }); var callsite = CallSite <Func <CallSite, object, object> > .Create(binder); return(callsite.Target(callsite, target)); }
public static Expression CompileGetMember(Expression target, string clientExpr) { var binder = DynamicBinder.GetMember(CSharpBinderFlags.None, clientExpr, typeof(DynamicBindingHelper), EMPTY_ARGUMENT_INFO); return(Expression.Call( typeof(Utils).GetMethod(nameof(Utils.UnwrapNewtonsoftValue)), DynamicExpression.Dynamic(binder, typeof(object), target) )); }
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; bool foundInCache; lock (_callSiteCacheLock) { foundInCache = DynamicInvokeCreateCallSite.TryGetValue(delegateType, out tSite); } if (!foundInCache) { 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 } )); lock (_callSiteCacheLock) { // another thread might have been faster; add to dictionary only if we are the first if (!DynamicInvokeCreateCallSite.ContainsKey(delegateType)) { DynamicInvokeCreateCallSite[delegateType] = tSite; } } } return((CallSite)tSite.Target(tSite, typeof(InvokeHelper), specificBinderType, knownType, binder, name, context, argNames, staticContext, isEvent)); }
public static T GetDynamicMemberValue <T>(this object obj, string name) { var dynamicObject = (DynamicJsonObject)obj; var binder = Binder.GetMember(CSharpBinderFlags.None, name, obj.GetType(), new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }); var callsite = CallSite <Func <CallSite, object, object> > .Create(binder); var value = callsite.Target(callsite, obj); return((T)value); }
public static Func <object, object> CallSiteBindGet(AccessorMember member) { var args = new List <CSharpArgumentInfo> { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }; var binder = Binder.GetMember(CSharpBinderFlags.None, member.Name, member.MemberInfo.DeclaringType, args); var callsite = CallSite <Func <CallSite, object, object> > .Create(binder); return(t => callsite.Target(callsite, t)); }
private static void AttemptExplicitCast <TFrom, TTo>() { // based on the IL generated from // var x = (TTo)(dynamic)default(TFrom); var binder = CSharpBinder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(TTo), typeof(ConversionHelper)); var callSite = CallSite <Func <CallSite, TFrom, TTo> > .Create(binder); callSite.Target(callSite, default(TFrom)); }
protected void SetDynamically(MemberInfo member, object target, object value) { var binder = Binder.SetMember(CSharpBinderFlags.None, member.Name, member.GetMemberType(), new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }); var callsite = CallSite <Func <CallSite, object, object, object> > .Create(binder); callsite.Target(callsite, target, value); }
public static Expression CompileGetMember(Expression target, string clientExpr) { if(EMPTY_ARGUMENT_INFO == null) EMPTY_ARGUMENT_INFO = new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }; var binder = DynamicBinder.GetMember(CSharpBinderFlags.None, clientExpr, typeof(DynamicBindingHelper), EMPTY_ARGUMENT_INFO); return Expression.Call( typeof(Utils).GetMethod(nameof(Utils.UnwrapNewtonsoftValue)), DynamicExpression.Dynamic(binder, typeof(object), target) ); }
private static void SetDynamically(string memberName, object target, object value) { var binder = Binder.SetMember(CSharpBinderFlags.None, memberName, null, new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }); var callsite = CallSite <Func <CallSite, object, object, object> > .Create(binder); callsite.Target(callsite, target, value); }