本文整理汇总了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;
}
示例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);
}
示例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);
}
示例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);
}
示例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());
}
示例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);
}
示例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();
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
}
示例12: BindConvert
public override DynamicMetaObject/*!*/ BindConvert(ConvertBinder/*!*/ action) {
var result = RubyBinder.TryBindCovertToDelegate(action, this);
if (result != null) {
return result;
}
return base.BindConvert(action);
}
示例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;
}
示例14: TryConvert
public override bool TryConvert(ConvertBinder binder, out object result)
{
if (binder.Type == typeof(bool))
{
}
return base.TryConvert(binder, out result);
}
示例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);
}