public override DynamicMetaObject BindConvert(ConvertBinder action) { if (action.Type.IsSubclassOf(typeof(Delegate))) { return MakeDelegateTarget(action, action.Type, Restrict(typeof(Method))); } return base.BindConvert(action); }
public override MetaObject/*!*/ BindConvert(ConvertBinder/*!*/ action) { var result = RubyBinder.TryBindCovertToDelegate(action, this); if (result != null) { return result; } return base.BindConvert(action); }
public override DynamicMetaObject BindConvert(ConvertBinder/*!*/ conversion) { PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "BuiltinFunc Convert " + conversion.Type); PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, "BuiltinFunc Convert"); if (conversion.Type.IsSubclassOf(typeof(Delegate))) { return MakeDelegateTarget(conversion, conversion.Type, Restrict(typeof(BuiltinFunction))); } return conversion.FallbackConvert(this); }
public override DynamicMetaObject/*!*/ BindConvert(ConvertBinder/*!*/ conversion) { Type type = conversion.Type; ValidationInfo typeTest = BindingHelpers.GetValidationInfo(Expression, Value.PythonType); return BindingHelpers.AddDynamicTestAndDefer( conversion, TryPythonConversion(conversion, type) ?? base.BindConvert(conversion), new DynamicMetaObject[] { this }, typeTest ); }
/// <summary>类型转换</summary> /// <param name="binder"></param> /// <param name="result"></param> /// <returns></returns> public override Boolean TryConvert(ConvertBinder binder, out Object result) { result = Real; return(true); }
protected abstract void WriteConvertBinder(ConvertBinder convertBinder, IList <Expression> args);
/// <summary> /// Override on DynamicObject /// </summary> /// <param name="binder"></param> /// <param name="result"></param> /// <returns></returns> public override bool TryConvert(ConvertBinder binder, out object result) { result = Impromptu.InvokeConstructor(binder.ReturnType); return(true); }
public override DynamicMetaObject BindConvert(ConvertBinder /*!*/ conversion) { return(ConvertWorker(conversion, conversion.Type, conversion.Type, conversion.Explicit ? ConversionResultKind.ExplicitCast : ConversionResultKind.ImplicitCast)); }
public override DynamicMetaObject BindConvert(ConvertBinder binder) { return(IsOverridden("TryConvert") ? CallMethodWithResult("TryConvert", binder, NoArgs, e => binder.FallbackConvert(this, e)) : base.BindConvert(binder)); }
private DynamicMetaObject/*!*/ MakeConvertToIEnumerable(ConvertBinder/*!*/ conversion, Type genericType) { ParameterExpression tmp = Ast.Variable(conversion.Type, "res"); DynamicMetaObject self = Restrict(typeof(OldInstance)); return new DynamicMetaObject( Ast.Block( new ParameterExpression[] { tmp }, Ast.Condition( Ast.NotEqual( Ast.Assign( tmp, Ast.Call( typeof(PythonOps).GetMethod("OldInstanceConvertToIEnumerableOfTNonThrowing").MakeGenericMethod(genericType), AstUtils.Constant(BinderState.GetBinderState(conversion).Context), self.Expression ) ), AstUtils.Constant(null) ), tmp, AstUtils.Convert( AstUtils.Convert( conversion.FallbackConvert(this).Expression, typeof(object) ), conversion.Type ) ) ), self.Restrictions ); }
private static Expression/*!*/ AddExtensibleSelfCheck(ConvertBinder/*!*/ convertToAction, DynamicMetaObject/*!*/ self, Expression/*!*/ callExpr) { ParameterExpression tmp = Ast.Variable(callExpr.Type, "tmp"); callExpr = Ast.Block( new ParameterExpression[] { tmp }, Ast.Block( Ast.Assign(tmp, callExpr), Ast.Condition( Ast.Equal(tmp, self.Expression), Ast.Property( AstUtils.Convert(self.Expression, self.GetLimitType()), self.GetLimitType().GetProperty("Value") ), Binders.Convert( BinderState.GetBinderState(convertToAction), convertToAction.Type, ConversionResultKind.ExplicitCast, tmp ) ) ) ); return callExpr; }
private DynamicMetaObject/*!*/ MakeConvertRuleForCall(ConvertBinder/*!*/ convertToAction, DynamicMetaObject/*!*/ self, SymbolId symbolId, string returner) { PythonType pt = ((IPythonObject)self.Value).PythonType; PythonTypeSlot pts; CodeContext context = BinderState.GetBinderState(convertToAction).Context; if (pt.TryResolveSlot(context, symbolId, out pts) && !IsBuiltinConversion(context, pts, symbolId, pt)) { ParameterExpression tmp = Ast.Variable(typeof(object), "func"); Expression callExpr = Ast.Call( PythonOps.GetConversionHelper(returner, GetResultKind(convertToAction)), Ast.Dynamic( new PythonInvokeBinder( BinderState.GetBinderState(convertToAction), new CallSignature(0) ), typeof(object), BinderState.GetCodeContext(convertToAction), tmp ) ); if (typeof(Extensible<>).MakeGenericType(convertToAction.Type).IsAssignableFrom(self.GetLimitType())) { // if we're doing a conversion to the underlying type and we're an // Extensible<T> of that type: // if an extensible type returns it's self in a conversion, then we need // to actually return the underlying value. If an extensible just keeps // returning more instances of it's self a stack overflow occurs - both // behaviors match CPython. callExpr = AstUtils.Convert(AddExtensibleSelfCheck(convertToAction, self, callExpr), typeof(object)); } return new DynamicMetaObject( Ast.Block( new ParameterExpression[] { tmp }, Ast.Condition( BindingHelpers.CheckTypeVersion( self.Expression, pt.Version ), Ast.Condition( MakeTryGetTypeMember( BinderState.GetBinderState(convertToAction), pts, self.Expression, tmp ), callExpr, AstUtils.Convert( ConversionFallback(convertToAction), typeof(object) ) ), convertToAction.Defer(this).Expression ) ), self.Restrict(self.GetRuntimeType()).Restrictions ); } return convertToAction.FallbackConvert(this); }
private DynamicMetaObject TryPythonConversion(ConvertBinder conversion, Type type) { if (!type.IsEnum) { switch (Type.GetTypeCode(type)) { case TypeCode.Object: if (type == typeof(Complex64)) { // TODO: Fallback to Float return MakeConvertRuleForCall(conversion, this, Symbols.ConvertToComplex, "ConvertToComplex"); } else if (type == typeof(BigInteger)) { return MakeConvertRuleForCall(conversion, this, Symbols.ConvertToLong, "ConvertToLong"); } else if (type == typeof(IEnumerable)) { return PythonProtocol.ConvertToIEnumerable(conversion, this); } else if (type == typeof(IEnumerator)){ return PythonProtocol.ConvertToIEnumerator(conversion, this); } else if (conversion.Type.IsSubclassOf(typeof(Delegate))) { return MakeDelegateTarget(conversion, conversion.Type, Restrict(Value.GetType())); } break; case TypeCode.Int32: return MakeConvertRuleForCall(conversion, this, Symbols.ConvertToInt, "ConvertToInt"); case TypeCode.Double: return MakeConvertRuleForCall(conversion, this, Symbols.ConvertToFloat, "ConvertToFloat"); case TypeCode.Boolean: return PythonProtocol.ConvertToBool( conversion, this ); } } return null; }
private DynamicMetaObject/*!*/ MakeConvertToBool(ConvertBinder/*!*/ conversion) { DynamicMetaObject self = Restrict(typeof(OldInstance)); ParameterExpression tmp = Ast.Variable(typeof(bool?), "tmp"); DynamicMetaObject fallback = conversion.FallbackConvert(this); Type resType = BindingHelpers.GetCompatibleType(typeof(bool), fallback.Expression.Type); return new DynamicMetaObject( Ast.Block( new ParameterExpression[] { tmp }, Ast.Condition( Ast.NotEqual( Ast.Assign( tmp, Ast.Call( typeof(PythonOps).GetMethod("OldInstanceConvertToBoolNonThrowing"), AstUtils.Constant(BinderState.GetBinderState(conversion).Context), self.Expression ) ), AstUtils.Constant(null) ), AstUtils.Convert(tmp, resType), AstUtils.Convert(fallback.Expression, resType) ) ), self.Restrictions ); }
private static BinaryExpression/*!*/ MakeOneConvert(ConvertBinder/*!*/ conversion, DynamicMetaObject/*!*/ self, SymbolId symbolId, ParameterExpression/*!*/ tmp) { return Ast.NotEqual( Ast.Assign( tmp, Ast.Call( typeof(PythonOps).GetMethod("OldInstanceConvertNonThrowing"), AstUtils.Constant(BinderState.GetBinderState(conversion).Context), self.Expression, AstUtils.Constant(symbolId) ) ), AstUtils.Constant(null) ); }
private DynamicMetaObject/*!*/ MakeConvertToCommon(ConvertBinder/*!*/ conversion, SymbolId symbolId) { ParameterExpression tmp = Ast.Variable(typeof(object), "convertResult"); DynamicMetaObject self = Restrict(typeof(OldInstance)); return new DynamicMetaObject( Ast.Block( new ParameterExpression[] { tmp }, Ast.Condition( MakeOneConvert(conversion, self, symbolId, tmp), tmp, AstUtils.Convert( conversion.FallbackConvert(this).Expression, typeof(object) ) ) ), self.Restrictions ); }
public override bool TryConvert(ConvertBinder binder, out object result) { result = null; if (binder.ReturnType == typeof(bool)) { result = (bool)this; } if (binder.ReturnType == typeof(bool?)) { result = (bool?)this; } if (binder.ReturnType == typeof(int)) { result = (int)this; } if (binder.ReturnType == typeof(int?)) { result = (int?)this; } if (binder.ReturnType == typeof(long)) { result = (long)this; } if (binder.ReturnType == typeof(long?)) { result = (long?)this; } if (binder.ReturnType == typeof(float)) { result = (float)this; } if (binder.ReturnType == typeof(float?)) { result = (float?)this; } if (binder.ReturnType == typeof(double)) { result = (double)this; } if (binder.ReturnType == typeof(double?)) { result = (double?)this; } if (binder.ReturnType == typeof(DateTime)) { result = (DateTime)this; } if (binder.ReturnType == typeof(DateTime?)) { result = (DateTime?)this; } if (binder.ReturnType == typeof(List <PyObject>)) { result = (List <PyObject>) this; } if (binder.ReturnType == typeof(List <int>)) { result = (List <int>) this; } if (binder.ReturnType == typeof(List <long>)) { result = (List <long>) this; } if (binder.ReturnType == typeof(List <string>)) { result = (List <string>) this; } if (binder.ReturnType == typeof(List <float>)) { result = ToList <float>(); } if (binder.ReturnType == typeof(List <double>)) { result = ToList <double>(); } if (binder.ReturnType == typeof(Dictionary <PyObject, PyObject>)) { result = ToDictionary <PyObject>(); } if (binder.ReturnType == typeof(Dictionary <int, PyObject>)) { result = ToDictionary <int>(); } if (binder.ReturnType == typeof(Dictionary <long, PyObject>)) { result = ToDictionary <long>(); } if (binder.ReturnType == typeof(Dictionary <string, PyObject>)) { result = ToDictionary <string>(); } if (binder.ReturnType == typeof(Dictionary <float, PyObject>)) { result = ToDictionary <float>(); } if (binder.ReturnType == typeof(Dictionary <double, PyObject>)) { result = ToDictionary <double>(); } return(result != null); }
public override bool TryConvert(ConvertBinder binder, out object result) { return(CurryConverter.TryConvert(this, binder, out result)); }
private ConversionResultKind GetResultKind(ConvertBinder convertToAction) { ConversionBinder cb = convertToAction as ConversionBinder; if (cb != null) { return cb.ResultKind; } if (convertToAction.Explicit) { return ConversionResultKind.ExplicitCast; } else { return ConversionResultKind.ImplicitCast; } }
public override bool TryConvert(ConvertBinder binder, out object result) { result = Activator.CreateInstance (binder.Type, BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { _name }, null); return(true); }
private Expression ConversionFallback(ConvertBinder/*!*/ convertToAction) { ConversionBinder cb = convertToAction as ConversionBinder; if (cb != null) { return GetConversionFailedReturnValue(cb, this); } return convertToAction.Defer(this).Expression; }
public override bool TryConvert(ConvertBinder binder, out object result) { result = Convert.ChangeType(RealObject, binder.Type); return(true); }
public new bool TryConvert(ConvertBinder binder, out object result) { if (binder.ReturnType == typeof(string)) { result = nameof(DynamicallyConvertable); return true; } if (binder.ReturnType == typeof(DateTime) && binder.Explicit) { result = new DateTime(1991, 8, 6); return true; } result = null; return false; }
public override DynamicMetaObject BindConvert(ConvertBinder binder) { return(IsOverridden(nameof(DynamicProxy <T> .TryConvert)) ? CallMethodWithResult(nameof(DynamicProxy <T> .TryConvert), binder, NoArgs, e => binder.FallbackConvert(this, e)) : base.BindConvert(binder)); }
public static bool TryConvert(ConvertBinder binder, DynamicMetaObject instance, out DynamicMetaObject result) { ContractUtils.RequiresNotNull(binder, "binder"); ContractUtils.RequiresNotNull(instance, "instance"); if (IsComObject(instance.Value)) { // // Demand Full Trust to proceed with the binding. // new PermissionSet(PermissionState.Unrestricted).Demand(); // Converting a COM object to any interface is always considered possible - it will result in // a QueryInterface at runtime if (binder.Type.IsInterface) { result = new DynamicMetaObject( Expression.Convert( instance.Expression, binder.Type ), BindingRestrictions.GetExpressionRestriction( Expression.Call( typeof(ComObject).GetMethod("IsComObject", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic), Helpers.Convert(instance.Expression, typeof(object)) ) ) ); return true; } } result = null; return false; }
public override DynamicMetaObject BindConvert( ConvertBinder binder ) { DEBUG.IndentLine( "\n-- BindConvert: {0}", binder.ReturnType.Name ); var obj = (DynamicNode)this.Value; var node = new DynamicNode.Convert( binder.ReturnType, obj ) { _Parser = obj._Parser }; obj._Parser._LastNode = node; // Reducing the object to return if this is an assignment node... object ret = obj; bool done = false; while( !done ) { if( ret is DynamicNode.SetMember ) ret = ( (DynamicNode.SetMember)obj ).Value; else if( ret is DynamicNode.SetIndex ) ret = ( (DynamicNode.SetIndex)obj ).Value; else done = true; } // Creating an instance... if( binder.ReturnType == typeof( string ) ) ret = ret.ToString(); else { try { if( TypeHelper.IsNullableType( binder.ReturnType ) ) ret = null; // to avoid cast exceptions else ret = Activator.CreateInstance( binder.ReturnType, true ); // true to allow non-public ctor as well } catch { ret = new object(); } // as the last resort scenario } var par = Expression.Variable( binder.ReturnType, "ret" ); var exp = Expression.Block( new ParameterExpression[] { par }, Expression.Assign( par, Expression.Constant( ret, binder.ReturnType ) ) // specifying binder.ReturnType ); DEBUG.Unindent(); return new DynamicMetaNode( exp, this.Restrictions, node ); }
/// <summary> /// Performs the binding of the dynamic conversion operation. /// </summary> /// <param name="binder">An instance of the <see cref="ConvertBinder"/> that represents the details of the dynamic operation.</param> /// <returns>The new <see cref="DynamicMetaObject"/> representing the result of the binding.</returns> public virtual DynamicMetaObject BindConvert(ConvertBinder binder) { ContractUtils.RequiresNotNull(binder, "binder"); return binder.FallbackConvert(this); }
public override bool TryConvert(ConvertBinder binder, out object result) { result = ToType(binder.Type, Thread.CurrentThread.CurrentCulture); return(true); }
public virtual bool TryConvert(ConvertBinder binder, out object result) { result = null; return false; }
public override bool TryConvert(ConvertBinder binder, out object result) { result = _concreteObject.Get(binder.Type, _data); return(result != null); }
public override DynamicMetaObject BindConvert(ConvertBinder binder) { if (IsOverridden("TryConvert")) { return CallMethodWithResult("TryConvert", binder, NoArgs, (e) => binder.FallbackConvert(this, e)); } return base.BindConvert(binder); }
public override bool TryConvert(ConvertBinder binder, out object result) { return(ConvertImpl(binder.Type, out result)); }
public sealed override bool TryConvert(ConvertBinder binder, out object result) { return(TryConvert(binder.ReturnType, out result)); }
public override bool TryConvert(ConvertBinder binder, out object result) { result = value; return(true); }
public override DynamicMetaObject BindConvert(ConvertBinder binder) { return(AddRestrictions(_metaForwardee.BindConvert(binder))); }
public override bool TryConvert(ConvertBinder binder, out object result) { return(Converter.ToManaged(this.obj, binder.Type, out result, false)); }
public override DynamicMetaObject/*!*/ BindConvert(ConvertBinder/*!*/ conversion) { Type type = conversion.Type; if (!type.IsEnum) { switch (Type.GetTypeCode(type)) { case TypeCode.Boolean: return MakeConvertToBool(conversion); case TypeCode.Int32: return MakeConvertToCommon(conversion, Symbols.ConvertToInt); case TypeCode.Double: return MakeConvertToCommon(conversion, Symbols.ConvertToFloat); case TypeCode.String: return MakeConvertToCommon(conversion, Symbols.String); case TypeCode.Object: if (type == typeof(BigInteger)) { return MakeConvertToCommon(conversion, Symbols.ConvertToLong); } else if (type == typeof(Complex64)) { return MakeConvertToCommon(conversion, Symbols.ConvertToComplex); } else if (type == typeof(IEnumerable)) { return MakeConvertToIEnumerable(conversion); } else if (type == typeof(IEnumerator)) { return MakeConvertToIEnumerator(conversion); } else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>)) { return MakeConvertToIEnumerable(conversion, type.GetGenericArguments()[0]); } else if (conversion.Type.IsSubclassOf(typeof(Delegate))) { return MakeDelegateTarget(conversion, conversion.Type, Restrict(typeof(OldInstance))); } break; } } return base.BindConvert(conversion); }
/// <summary> /// Provides implementation for type conversion operations. Classes derived from the <see cref="T:System.Dynamic.DynamicObject" /> class can /// override this method to specify dynamic behavior for operations that convert an object from one type to another. /// </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 conversion operation. The binder.Type property provides the type to which the object /// must be converted. For example, for the statement (String)sampleObject in C# (CType(sampleObject, Type) in Visual Basic), where /// sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject" /> class, binder.Type returns the /// <see cref="T:System.String" /> type. The binder.Explicit property provides information about the kind of conversion that occurs. It returns /// true for explicit conversion and false for implicit conversion. /// </param> /// <param name="result">The result of the type conversion operation.</param> public override bool TryConvert(ConvertBinder binder, out object result) { result = null; if (_value == null) { return(true); } var binderType = binder.Type; if (binderType == typeof(string)) { result = Convert.ToString(_value); return(true); } if (binderType == typeof(Guid) || binderType == typeof(Guid?)) { Guid guid; if (Guid.TryParse(Convert.ToString(_value), out guid)) { result = guid; return(true); } } else if (binderType == typeof(TimeSpan) || binderType == typeof(TimeSpan?)) { TimeSpan timespan; if (TimeSpan.TryParse(Convert.ToString(_value), out timespan)) { result = timespan; return(true); } } else { if (binderType.IsGenericType && binderType.GetGenericTypeDefinition() == typeof(Nullable <>)) { binderType = binderType.GetGenericArguments()[0]; } var typeCode = Type.GetTypeCode(binderType); if (typeCode == TypeCode.Object) { if (binderType.IsAssignableFrom(_value.GetType())) { result = _value; return(true); } else { return(false); } } result = Convert.ChangeType(_value, typeCode); return(true); } return(base.TryConvert(binder, out result)); }
internal static DynamicMetaObject TryBindCovertToDelegate(ConvertBinder/*!*/ action, DynamicMetaObject/*!*/ target) { if (typeof(Delegate).IsAssignableFrom(action.Type)) { return new DynamicMetaObject( Methods.CreateDelegateFromMethod.OpCall( Ast.Constant(action.Type), AstUtils.Convert(target.Expression, typeof(RubyMethod)) ), target.Restrictions.Merge(BindingRestrictions.GetTypeRestriction(target.Expression, target.Value.GetType())) ); } return null; }
public virtual bool TryConvert(ConvertBinder binder, out object result) { Console.WriteLine("System.Windows.Interop.ComAutomationMetaObjectProviderBase.TryConvert: NIEX"); throw new NotImplementedException(); }
public override DynamicMetaObject/*!*/ BindConvert(ConvertBinder/*!*/ conversion) { if (conversion.Type.IsSubclassOf(typeof(Delegate))) { return MakeDelegateTarget(conversion, conversion.Type, Restrict(typeof(PythonFunction))); } return conversion.FallbackConvert(this); }
public ForgivingConvertBinder(ConvertBinder innerBinder) : base(innerBinder.ReturnType, innerBinder.Explicit) { _innerBinder = innerBinder; }
/// <summary> /// Provides implementation for type conversion operations. Classes derived from the <see cref="T:System.Dynamic.DynamicObject"/> class can override this method to specify dynamic behavior for operations that convert an object from one type to another. /// </summary> /// <returns><see langword="true"/> if the operation is successful; otherwise, <see langword="false"/>. If this method returns <see langword="false"/>, 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 conversion operation. The binder.Type property provides the type to which the object must be converted. For example, for the statement (String)sampleObject in C# (CType(sampleObject, Type) in Visual Basic), where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject"/> class, binder.Type returns the <see cref="T:System.String"/> type. The binder.Explicit property provides information about the kind of conversion that occurs. It returns true for explicit conversion and false for implicit conversion.</param><param name="result">The result of the type conversion operation.</param> public override bool TryConvert(ConvertBinder binder, out object result) { result = null; if (this.value == null) { return(true); } var binderType = binder.Type; if (binderType == typeof(string)) { result = Convert.ToString(this.value); return(true); } if (binderType == typeof(Guid) || binderType == typeof(Guid?)) { Guid guid; if (Guid.TryParse(Convert.ToString(this.value), out guid)) { result = guid; return(true); } } else if (binderType == typeof(TimeSpan) || binderType == typeof(TimeSpan?)) { TimeSpan timespan; if (TimeSpan.TryParse(Convert.ToString(this.value), out timespan)) { result = timespan; return(true); } } else if (binderType.GetTypeInfo().IsEnum) { // handles enum to enum assignments if (value.GetType().GetTypeInfo().IsEnum) { if (binderType == this.value.GetType()) { result = this.value; return(true); } return(false); } // handles number to enum assignments if (Enum.GetUnderlyingType(binderType) == this.value.GetType()) { result = Enum.ToObject(binderType, this.value); return(true); } return(false); } else { if (binderType.GetTypeInfo().IsGenericType&& binderType.GetGenericTypeDefinition() == typeof(Nullable <>)) { binderType = binderType.GetGenericArguments()[0]; } var typeCode = binderType.GetTypeCode(); if (typeCode == TypeCode.Object) { if (binderType.IsAssignableFrom(this.value.GetType())) { result = this.value; return(true); } else { return(false); } } #if !NETSTANDARD1_6 result = Convert.ChangeType(value, typeCode); #else result = Convert.ChangeType(value, binderType); #endif return(true); } return(base.TryConvert(binder, out result)); }
internal static Delegate CreateDelegateForDynamicObject(LanguageContext context, object dynamicObject, Type delegateType, MethodInfo invoke) { PerfTrack.NoteEvent(PerfTrack.Categories.DelegateCreate, delegateType.ToString()); Type returnType = invoke.ReturnType; ParameterInfo[] parameterInfos = invoke.GetParameters(); var parameters = new List <ParameterExpression>(); for (int i = 0; i < parameterInfos.Length; i++) { parameters.Add(Expression.Parameter(parameterInfos[i].ParameterType, "p" + i)); } InvokeBinder invokeBinder = context.CreateInvokeBinder(new CallInfo(parameterInfos.Length)); ConvertBinder convertBinder = (returnType != typeof(void)) ? context.CreateConvertBinder(returnType, explicitCast: true) : null; CallSite invokeSite = CallSite.Create(DynamicSiteHelpers.MakeCallSiteDelegate(MakeSiteSignature(parameterInfos)), invokeBinder); Type invokeSiteType = invokeSite.GetType(); Type convertSiteType; CallSite convertSite; if (convertBinder != null) { convertSite = CallSite.Create(DynamicSiteHelpers.MakeCallSiteDelegate(typeof(object), returnType), convertBinder); convertSiteType = convertSite.GetType(); } else { convertSiteType = null; convertSite = null; } var locals = new List <ParameterExpression>(); ParameterExpression invokeSiteVar = Expression.Parameter(invokeSiteType, "site"); ParameterExpression convertSiteVar = null; var args = new List <Expression>(); args.Add(invokeSiteVar); args.Add(Expression.Constant(dynamicObject)); int strongBoxVarsStart = locals.Count; for (int i = 0; i < parameterInfos.Length; i++) { if (parameterInfos[i].ParameterType.IsByRef) { var argType = parameterInfos[i].ParameterType; Type elementType = argType.GetElementType(); Type concreteType = typeof(StrongBox <>).MakeGenericType(elementType); var strongBox = Expression.Parameter(concreteType, "box" + i); locals.Add(strongBox); args.Add( Expression.Assign( strongBox, Expression.New( concreteType.GetConstructor(new Type[] { elementType }), parameters[i] ) ) ); } else { args.Add(parameters[i]); } } int strongBoxVarsEnd = locals.Count; Expression invocation = Expression.Invoke( Expression.Field( Expression.Assign( invokeSiteVar, Expression.Convert(Expression.Constant(invokeSite), invokeSiteType) ), invokeSiteType.GetDeclaredField("Target") ), args ); if (convertBinder != null) { convertSiteVar = Expression.Parameter(convertSiteType, "convertSite"); invocation = Expression.Invoke( Expression.Field( Expression.Assign( convertSiteVar, Expression.Convert(Expression.Constant(convertSite), convertSiteType) ), convertSiteType.GetDeclaredField("Target") ), convertSiteVar, invocation ); } locals.Add(invokeSiteVar); if (convertSiteVar != null) { locals.Add(convertSiteVar); } Expression body; // copy back from StrongBox.Value if (strongBoxVarsEnd > strongBoxVarsStart) { var block = new Expression[1 + strongBoxVarsEnd - strongBoxVarsStart + 1]; var resultVar = Expression.Parameter(invocation.Type, "result"); locals.Add(resultVar); int b = 0; int l = strongBoxVarsStart; // values of strong boxes are initialized in invocation expression: block[b++] = Expression.Assign(resultVar, invocation); for (int i = 0; i < parameterInfos.Length; i++) { if (parameterInfos[i].ParameterType.IsByRef) { var local = locals[l++]; block[b++] = Expression.Assign( parameters[i], Expression.Field(local, local.Type.GetDeclaredField("Value")) ); } } block[b++] = resultVar; Debug.Assert(l == strongBoxVarsEnd); Debug.Assert(b == block.Length); body = Expression.Block(locals, block); } else { body = Expression.Block(locals, invocation); } var lambda = Expression.Lambda(delegateType, body, "_Scripting_", parameters); return(lambda.Compile()); }
public override bool TryConvert(ConvertBinder binder, out object result) { result = JsonSerializer.DeserializeFromString(jsonObject.First().Value, binder.ReturnType); return(true); }
public override bool TryConvert(ConvertBinder binder, out object result) { result = ConvertTo.Convert(_data, binder.Type); return(true); }
public override bool TryConvert(ConvertBinder binder, out object result) { return(((MemberAccessWrapper)MemberAccessWrapper).TryConvert(binder, out result)); }
public override DynamicMetaObject BindConvert(ConvertBinder binder) { return(this.forward.BindConvert(binder)); }
public virtual bool TryConvert( ConvertBinder binder, out object result);
public virtual bool TryConvert(T instance, ConvertBinder binder, out object result) { result = null; return(false); }
/// <summary> /// Provides implementation for type conversion operations. Classes derived from the <see cref="T:System.Dynamic.DynamicObject"/> class can override this method to specify dynamic behavior for operations that convert an object from one type to another. /// </summary> /// <param name="binder">Provides information about the conversion operation. The binder.Type property provides the type to which the object must be converted. For example, for the statement (String)sampleObject in C# (CType(sampleObject, Type) in Visual Basic), where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject"/> class, binder.Type returns the <see cref="T:System.String"/> type. The binder.Explicit property provides information about the kind of conversion that occurs. It returns true for explicit conversion and false for implicit conversion.</param> /// <param name="result">The result of the type conversion operation.</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 TryConvert(ConvertBinder binder, out object result) { result = Impromptu.CoerceToDelegate(this, binder.Type); return(result != null); }
public override DynamicMetaObject BindConvert(ConvertBinder/*!*/ conversion) { return ConvertWorker(conversion, conversion.Type, conversion.Type, conversion.Explicit ? ConversionResultKind.ExplicitCast : ConversionResultKind.ImplicitCast); }
public override DynamicMetaObject BindConvert(ConvertBinder binder) { return(base.BindConvert(binder)); }