Represents the convert dynamic operation at the call site, providing the binding semantic and the details about the operation.
Наследование: DynamicMetaObjectBinder
Пример #1
0
        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            // string へのキャストで、要素の値を取得。
            if (binder.Type == typeof(string))
            {
                result = element.Value;
                return true;
            }
            // int へのキャストで int.Parse。
            // Parse できないときは例外丸投げ。
            if (binder.Type == typeof(int))
            {
                result = int.Parse(element.Value);
                return true;
            }

            // 要素単体に対して foreach やっちゃったときでもエラーにならないように、IEnumerable へのキャストを定義。
            // これやっとかないと、元々複数要素あったのに XML を修正して要素が1個だけになった時に挙動おかしくなる。
            if (binder.Type == typeof(System.Collections.IEnumerable))
            {
                result = new[] { this };
                return true;
            }

            result = null;
            return false;
        }
Пример #2
0
        protected static bool TryConvert( ConvertBinder binder, DynamicMetaObject instance, out DynamicMetaObject result )
        {
            if ( instance.HasValue &&
                 instance.RuntimeType.IsValueType )
            {
                result = instance.BindConvert( binder );
                return true;
            }

            if ( binder.Type.IsInterface )
            {
                Expression expression = Convert( instance.Expression, binder.Type );
                result = new DynamicMetaObject( expression, BindingRestrictions.Empty, instance.Value );
                result = result.BindConvert( binder );
                return true;
            }

            if ( typeof (IDynamicMetaObjectProvider).IsAssignableFrom( instance.RuntimeType ) )
            {
                result = instance.BindConvert( binder );
                return true;
            }

            result = null;
            return false;
        }
Пример #3
0
        internal DelegateInfo(LanguageContext context, Type returnType, ParameterInfo[] parameters) {
            Assert.NotNull(returnType);
            Assert.NotNullItems(parameters);

            _returnType = returnType;
            _parameters = parameters;

            PerfTrack.NoteEvent(PerfTrack.Categories.DelegateCreate, ToString());

            if (_returnType != typeof(void)) {
                _convertBinder = context.CreateConvertBinder(_returnType, true);
            }

            _invokeBinder = context.CreateInvokeBinder(new CallInfo(_parameters.Length));

            Type[] delegateParams = new Type[_parameters.Length];
            for (int i = 0; i < _parameters.Length; i++) {
                delegateParams[i] = _parameters[i].ParameterType;
            }

            // Create the method with a special name so the langauge compiler knows that method's stack frame is not visible
            DynamicILGen cg = Snippets.Shared.CreateDynamicMethod("_Scripting_", _returnType, ArrayUtils.Insert(typeof(object[]), delegateParams), false);

            // Emit the stub
            _constants = EmitClrCallStub(cg);
            _method = cg.Finish();
        }
        /// <summary>
        /// Implements dynamic cast for JsonValue types.
        /// </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 override DynamicMetaObject BindConvert(ConvertBinder binder)
        {
            if (binder == null)
            {
                throw new ArgumentNullException("binder");
            }

            Expression expression = Expression;

            bool implicitCastSupported =
                binder.Type.IsAssignableFrom(LimitType) ||
                binder.Type == typeof(IEnumerable<KeyValuePair<string, JsonValue>>) ||
                binder.Type == typeof(IDynamicMetaObjectProvider) ||
                binder.Type == typeof(object);

            if (!implicitCastSupported)
            {
                if (JsonValue.IsSupportedExplicitCastType(binder.Type))
                {
                    Expression instance = Expression.Convert(Expression, LimitType);
                    expression = Expression.Call(_castValueMethodInfo.MakeGenericMethod(binder.Type), new Expression[] { instance });
                }
                else
                {
                    string exceptionMessage = RS.Format(Properties.Resources.CannotCastJsonValue, LimitType.FullName, binder.Type.FullName);
                    expression = Expression.Throw(Expression.Constant(new InvalidCastException(exceptionMessage)), typeof(object));
                }
            }

            expression = Expression.Convert(expression, binder.Type);

            return new DynamicMetaObject(expression, DefaultRestrictions);
        }
 public override bool TryConvert(ConvertBinder binder, out object result)
 {
     if (binder.ReturnType == typeof(bool))
     {
         result = bool.Parse(_value);
     }
     else if (binder.ReturnType == typeof(int))
     {
         result = int.Parse(_value);
     }
     else if (binder.ReturnType == typeof(float))
     {
         result = float.Parse(_value);
     }
     else if (binder.ReturnType == typeof(string))
     {
         result = _value;
     }
     else if (binder.ReturnType == typeof(DateTime))
     {
         result = DateTime.Parse(_value);
     }
     else
     {
         throw new Exception("Cannot convert string to type " + binder.ReturnType);
     }
     return true;
 }
        /// <summary>
        ///  If you don't call a method that invokes content you will need to dispose HttpContent, for Json this is done for you
        /// https://aspnetwebstack.codeplex.com/discussions/461495
        /// </summary>
        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            if (!_isRoot)
            {
                throw new InvalidOperationException("Sorry implict cast not supported on child objects yet!");
            }

            if (binder.Type == typeof(IEnumerable) && _currentObject is JArray)
            {
                result = Extensions.WrapJToken(_currentObject);
                return true;
            }

            if (binder.Type == typeof(HttpResponseMessage))
            {
                result = _httpResponseMessage;
                return true;
            }

            if (_isJson)
            {
                var isValid = ToString().TryParseJson(out result, binder.Type);
                return isValid;
            }

            throw new InvalidOperationException("Can not cast to " + binder.Type.FullName + OutputErrorString());
        }
Пример #7
0
        public override DynamicMetaObject BindConvert(ConvertBinder action) {
            if (action.Type.IsSubclassOf(typeof(Delegate))) {
                return MakeDelegateTarget(action, action.Type, Restrict(typeof(Method)));
            }

            return base.BindConvert(action);
        }
Пример #8
0
        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            var eng = GetEngine();
            if (binder.Type == typeof(int))
            {
                result = eng.Converter.ToInt32(this);
                return true;
            }
            else if (binder.Type == typeof(double))
            {
                result = eng.Converter.ToDouble(this);
                return true;
            }
            else if (binder.Type == typeof(string))
            {
                result = eng.Converter.ToString(this);
                return true;
            }
            else if (binder.Type == typeof(bool))
            {
                result = eng.Converter.ToBoolean(this);
                return true;
            }

            return base.TryConvert(binder, out result);
        }
Пример #9
0
        /// <summary>
        /// Tries to convert the current instance.
        /// </summary>
        /// <param name="binder">The binder.</param>
        /// <param name="result">The result.</param>
        /// <returns></returns>
        public override bool TryConvert(System.Dynamic.ConvertBinder binder, out object result)
        {
            var targetType = binder.Type;
            var isExplicit = binder.Explicit;

            result = null;
            object tempResult;

            if (RemoteInvoke(new Invocation(InvocationKind.Convert, (isExplicit ? "op_Explicit" : "op_Implicit"), targetType, isExplicit), out tempResult))
            {
                if (tempResult.GetType() == targetType)
                { // Not wrapped, so most likely a primitive?
                    result = tempResult;
                }
                else
                {
                    if (targetType.IsInterface)
                    {
                        result = Impromptu.DynamicActLike(tempResult, targetType);
                    }
                    else
                    {
                        // We can not present ourself as any class so we just try keep beeing dynamic.
                        result = tempResult;
                    }
                }
                return(true);
            }
            return(false);
        }
Пример #10
0
        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            if (_converter.HasConverters && _converter.TryConversion(binder.Type, _dataDictionary, out result))
                return true;

            return base.TryConvert(binder, out result);
        }
        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            if (binder.ReturnType.IsValueType)
                result = Activator.CreateInstance(binder.ReturnType);
            else
                result = null;

            if (binder.ReturnType.IsValueType || binder.ReturnType == typeof(string))
            {
                try
                {
                    result = System.Convert.ChangeType(dynamicNodes.Single().ToString(), binder.ReturnType);
                }
                catch
                {
                    return false;
                }
            }
            else if (binder.ReturnType.IsArray || typeof(System.Array).IsAssignableFrom(binder.ReturnType))
            {
                if (binder.ReturnType.GetElementType() == null)
                    result = dynamicNodes.ToArray();
                else
                {
                    var temp = dynamicNodes
                    .Select(node =>
                    {
                        object value = null;
                        if (binder.ReturnType.IsValueType)
                            value = Activator.CreateInstance(binder.ReturnType);
                        else
                            value = null;
                        try
                        {
                            value = System.Convert.ChangeType(node.ToString(), binder.ReturnType.GetElementType());
                        }
                        catch { }

                        return value;
                    })
                    .Where(value => value != null)
                    .ToArray();

                    var array = Array.CreateInstance(binder.ReturnType.GetElementType(), temp.Count());
                    temp.CopyTo(array, 0);
                    result = array;
                }
            }
            else if (typeof(IEnumerable).IsAssignableFrom(binder.ReturnType))
            {
                result = this;
            }
            else
            {
                return false;
            }

            return true;
        }
Пример #12
0
 public override DynamicMetaObject/*!*/ BindConvert(ConvertBinder/*!*/ binder) {
     var protocolConversion = ProtocolConversionAction.TryGetDefaultConversionAction(Context, binder.Type);
     if (protocolConversion != null) {
         return protocolConversion.Bind(this, DynamicMetaObject.EmptyMetaObjects);
     } else {
         return binder.FallbackConvert(this);
     }
 }
Пример #13
0
            public override DynamicMetaObject/*!*/ BindConvert(ConvertBinder/*!*/ action) {
                var result = RubyBinder.TryBindCovertToDelegate(action, this);
                if (result != null) {
                    return result;
                }

                return base.BindConvert(action);
            }
Пример #14
0
        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            if (binder.Type == typeof(bool))
            {

            }
            return base.TryConvert(binder, out result);
        }
Пример #15
0
        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            result = binder.Type.IsEnum
                ? Enum.Parse(binder.Type, node.Value)
                : Convert.ChangeType(node.Value, binder.Type, CultureInfo.InvariantCulture);

            return true;
        }
Пример #16
0
 public override bool TryConvert(ConvertBinder binder, out object result)
 {
     result = null;
     if(binder.Type == typeof(IEnumerable) && IsArrayList)
     {
         result = ((ArrayList)obj).ToArray().Select(o => new JsonDynamicDocument(o));
     }
     return true;
 }
Пример #17
0
		public override bool TryConvert(ConvertBinder binder, out object result)
		{
			//--- Debbuger Break --- //
			if(System.Diagnostics.Debugger.IsAttached)
				System.Diagnostics.Debugger.Break();
			//--- Debbuger Break --- //

			return base.TryConvert(binder, out result);
		}
Пример #18
0
 public override bool TryConvert(ConvertBinder binder, out object result)
 {
     if (binder.ReturnType == typeof(string))
     {
         result = _elements[0].Value;
         return true;
     }
     return base.TryConvert(binder, out result);
 }
Пример #19
0
        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            if (binder.Type == typeof(string))
                result = m_value.ToString();
            else
                result = Convert.ChangeType(m_value, binder.Type);

            return true;
        }
 public override bool TryConvert(ConvertBinder binder, out object result)
 {
     if (binder.ReturnType == Model.GetType())
     {
         result = Model;
         return true;
     }
     return base.TryConvert(binder, out result);
 }
Пример #21
0
        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            var mapper = typeMap.GetOrDefault(binder.Type);
            if (mapper == null)
                return base.TryConvert(binder, out result);

            result = mapper(element);
            return true;
        }
Пример #22
0
 public override bool TryConvert(ConvertBinder binder, out object result)
 {
     Type targetType = binder.Type;
     if (targetType == typeof(IEnumerable)) {
         result = this;
         return true;
     }
     return base.TryConvert(binder, out result);
 }
Пример #23
0
        /// <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 = null;
              if (_wrapped == null || !binder.Type.IsAssignableFrom(_wrapped.GetType()))
            return false;

              result = _wrapped;
              return true;
        }
Пример #24
0
 public override bool TryConvert(System.Dynamic.ConvertBinder binder, out object?result)
 {
     if (!binder.Type.IsAssignableFrom(this.type))
     {
         return(base.TryConvert(binder, out result));                // let the base call throw the exception
     }
     result = this._obj;
     return(true);
 }
Пример #25
0
            public override bool TryConvert(ConvertBinder binder, out object result)
            {
                if (binder.ReturnType == typeof(string))
                {
                    result = this.textResource[this.memberName, this.context];
                    return true;
                }

                throw new InvalidOperationException("Cannot cast dynamic member access to anything else than a string.");
            }
Пример #26
0
 public override bool TryConvert(ConvertBinder binder, out object result)
 {
     if (binder.Type == typeof(IEditableObject))
     {
         result = this;
         return true;
     }
     else
         return base.TryConvert(binder, out result);
 }
Пример #27
0
        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            if (binder.Type.IsAssignableFrom(this.value.GetType()))
            {
                result = this.value;
                return true;
            }

            return base.TryConvert(binder, out result);
        }
Пример #28
0
 public override bool TryConvert(ConvertBinder binder, out object result) {
     result = null;
     if (binder.Type.IsAssignableFrom(_values.GetType())) {
         result = _values;
     }
     else {
         throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, HelpersResources.Json_UnableToConvertType, binder.Type));
     }
     return true;
 }
Пример #29
0
        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            result = null;

            if (!binder.Type.IsAssignableFrom(_model.GetType()))
                throw new InvalidOperationException(String.Format(@"Unable to convert to ""{0}"".", binder.Type));

            result = _model;
            return true;
        }
Пример #30
0
        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            if(binder.ReturnType != typeof(String[])) {
                result = null;
                return false;
            }

            result = _data;
            return true;
        }
            public override DynamicMetaObject BindConvert(ConvertBinder binder)
            {
                var value = this.HasValue
                    ? (this.Value as ODataEntry).AsDictionary().ToObject(binder.Type)
                    : null;

                return new DynamicMetaObject(
                    Expression.Constant(value),
                    BindingRestrictions.GetTypeRestriction(Expression, LimitType));
            }
Пример #32
0
        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            if (binder.Type == typeof(ClrType))
            {
                result = m_type;
                return true;
            }

            return base.TryConvert(binder, out result);
        }
Пример #33
0
 /// <summary>
 /// Forwards the convert operation.
 /// </summary>
 /// <param name="binder">the binder</param>
 /// <param name="result">the result</param>
 /// <returns>true when successfull</returns>
 public override bool TryConvert(System.Dynamic.ConvertBinder binder, out object result)
 {
     result = null;
     try
     {
         result = Impromptu.InvokeConvert(CallTarget, binder.Type, binder.Explicit);
         return(true);
     }
     catch (RuntimeBinderException)
     {
         return(false);
     }
 }
Пример #34
0
        public override bool TryConvert(System.Dynamic.ConvertBinder binder, out object result)
        {
            result = null;

            if (binder.Type.IsInterface)
            {
                this._interfaceTypes.Insert(0, binder.Type);
                result = Impromptu.DynamicActLike(this.Target, this._interfaceTypes.ToArray());
                return(true);
            }

            if (binder.Type.IsInstanceOfType(this.Target))
            {
                result = this.Target;
            }

            return(false);
        }
Пример #35
0
        public virtual new System.Dynamic.DynamicMetaObject BindConvert(ConvertBinder binder)
        {
            Contract.Requires(binder != null);

            return(default(System.Dynamic.DynamicMetaObject));
        }
 //
 // Summary:
 //     Provides implementation for type conversion operations. Classes derived from
 //     the System.Dynamic.DynamicObject class can override this method to specify dynamic
 //     behavior for operations that convert an object from one type to another.
 //
 // Parameters:
 //   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 System.Dynamic.DynamicObject
 //     class, binder.Type returns the 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.
 //
 //   result:
 //     The result of the type conversion operation.
 //
 // 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.)
 public virtual bool TryConvert(ConvertBinder binder, out object result);
Пример #37
0
 public virtual DynamicMetaObject BindConvert(ConvertBinder binder)
 {
     throw new NotImplementedException();
 }
Пример #38
0
 public virtual bool TryConvert(ConvertBinder binder, out object result)
 {
     result = null;
     return(false);
 }
Пример #39
0
 /// <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)
 {
     ArgumentNullException.ThrowIfNull(binder);
     return(binder.FallbackConvert(this));
 }
Пример #40
0
 /// <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));
 }
Пример #41
0
 public override bool TryConvert(System.Dynamic.ConvertBinder binder, out object result)
 {
     result = this;
     return(true);
 }
Пример #42
0
 public virtual bool TryConvert(ConvertBinder binder, ref object result)
 {
     throw new NotImplementedException();
 }
        public virtual new bool TryConvert(ConvertBinder binder, out Object result)
        {
            result = default(Object);

            return(default(bool));
        }
Пример #44
0
 /// <summary>
 ///     Provides the implementation of converting the <see cref="DynamicObject" /> to another type.
 ///     Derived classes can override this method to customize behavior.  When not overridden the
 ///     call site requesting the binder determines the behavior.
 /// </summary>
 /// <param name="binder">The binder provided by the call site.</param>
 /// <param name="result">The result of the conversion.</param>
 /// <returns>true if the operation is complete, false if the call site should determine behavior.</returns>
 public virtual bool TryConvert(ConvertBinder binder, [NotNullWhen(true)] out object?result)
 {
     _      = binder;
     result = null;
     return(false);
 }