当前位置: 首页>>代码示例>>C#>>正文


C# Dynamic.ConvertBinder类代码示例

本文整理汇总了C#中System.Dynamic.ConvertBinder的典型用法代码示例。如果您正苦于以下问题:C# ConvertBinder类的具体用法?C# ConvertBinder怎么用?C# ConvertBinder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ConvertBinder类属于System.Dynamic命名空间,在下文中一共展示了ConvertBinder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TryConvert

 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;
 }
开发者ID:maxfridbe,项目名称:Infrastructure,代码行数:28,代码来源:DynamicStringProperty.cs

示例2: BindConvert

        public override DynamicMetaObject BindConvert(ConvertBinder action) {
            if (action.Type.IsSubclassOf(typeof(Delegate))) {
                return MakeDelegateTarget(action, action.Type, Restrict(typeof(Method)));
            }

            return base.BindConvert(action);
        }
开发者ID:octavioh,项目名称:ironruby,代码行数:7,代码来源:MetaMethod.cs

示例3: TryConvert

        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);
        }
开发者ID:frabert,项目名称:jsrt-dotnet,代码行数:26,代码来源:JavaScriptValue.cs

示例4: TryConvert

        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);
        }
开发者ID:nick-randal,项目名称:UsefulCSharp,代码行数:7,代码来源:DynamicEntity.cs

示例5: TryConvert

        /// <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());
        }
开发者ID:MinChanSike,项目名称:DalSoft.RestClient,代码行数:31,代码来源:RestClientResponseObject.cs

示例6: BindConvert

        /// <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);
        }
开发者ID:JokerMisfits,项目名称:linux-packaging-mono,代码行数:38,代码来源:JsonValueDynamicMetaObject.cs

示例7: DelegateInfo

        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();
        }
开发者ID:BenHall,项目名称:ironruby,代码行数:27,代码来源:DelegateInfo.cs

示例8: TryConvert

        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;
        }
开发者ID:ufcpp,项目名称:UfcppSample,代码行数:27,代码来源:DynamicXml.cs

示例9: TryConvert

        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;
        }
开发者ID:idavis,项目名称:Archetype,代码行数:26,代码来源:NestedCastingMetaObject.cs

示例10: TryConvert

        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;
        }
开发者ID:dialectsoftware,项目名称:DialectSoftware.Xml,代码行数:59,代码来源:DynamicConverter.cs

示例11: BindConvert

 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);
     }
 }
开发者ID:ExpertsInside,项目名称:IronSP,代码行数:8,代码来源:RubyMetaObject.cs

示例12: BindConvert

            public override DynamicMetaObject/*!*/ BindConvert(ConvertBinder/*!*/ action) {
                var result = RubyBinder.TryBindCovertToDelegate(action, this);
                if (result != null) {
                    return result;
                }

                return base.BindConvert(action);
            }
开发者ID:joshholmes,项目名称:ironruby,代码行数:8,代码来源:RubyMethod.Meta.cs

示例13: TryConvert

        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;
        }
开发者ID:cg123,项目名称:xenko,代码行数:8,代码来源:DynamicYamlScalar.cs

示例14: TryConvert

        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            if (binder.Type == typeof(bool))
            {

            }
            return base.TryConvert(binder, out result);
        }
开发者ID:meadiagenic,项目名称:Charismatic,代码行数:8,代码来源:DynamicValue.cs

示例15: TryConvert

		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);
		}
开发者ID:GoldMax,项目名称:Pulsar.NET,代码行数:9,代码来源:DynamicInvokeObject.cs


注:本文中的System.Dynamic.ConvertBinder类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。