本文整理汇总了C#中Lens.Compiler.Context.ResolveConvertorToType方法的典型用法代码示例。如果您正苦于以下问题:C# Context.ResolveConvertorToType方法的具体用法?C# Context.ResolveConvertorToType怎么用?C# Context.ResolveConvertorToType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Lens.Compiler.Context
的用法示例。
在下文中一共展示了Context.ResolveConvertorToType方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: emitCode
protected override void emitCode(Context ctx, bool mustReturn)
{
var gen = ctx.CurrentMethod.Generator;
var fromType = Expression.Resolve(ctx);
var toType = Resolve(ctx);
if (toType.IsExtendablyAssignableFrom(fromType, true))
Expression.Emit(ctx, true);
else if (fromType.IsNumericType() && toType.IsNumericType(true)) // (decimal -> T) is processed via op_Explicit()
castNumeric(ctx, fromType, toType);
else if(fromType.IsCallableType() && toType.IsCallableType())
castDelegate(ctx, fromType, toType);
else if (fromType == typeof (NullType))
{
if (toType.IsNullableType())
{
var tmpVar = ctx.Scope.DeclareImplicit(ctx, toType, true);
gen.EmitLoadLocal(tmpVar.LocalBuilder, true);
gen.EmitInitObject(toType);
gen.EmitLoadLocal(tmpVar.LocalBuilder);
}
else if (!toType.IsValueType)
gen.EmitNull();
else
error(CompilerMessages.CastNullValueType, toType);
}
else if (toType.IsNullableType())
{
Expression.Emit(ctx, true);
var underlying = Nullable.GetUnderlyingType(toType);
if(underlying.IsNumericType() && fromType.IsNumericType() && underlying != fromType)
gen.EmitConvert(underlying);
else if(underlying != fromType)
error(fromType, toType);
var ctor = toType.GetConstructor(new[] { underlying });
gen.EmitCreateObject(ctor);
}
else if (toType.IsExtendablyAssignableFrom(fromType))
{
Expression.Emit(ctx, true);
// box
if (fromType.IsValueType && toType == typeof (object))
gen.EmitBox(fromType);
else
{
var castOp = ctx.ResolveConvertorToType(fromType, toType);
if (castOp != null)
gen.EmitCall(castOp.MethodInfo);
else
gen.EmitCast(toType);
}
}
else if (fromType.IsExtendablyAssignableFrom(toType))
{
Expression.Emit(ctx, true);
// unbox
if (fromType == typeof (object) && toType.IsValueType)
gen.EmitUnbox(toType);
// cast ancestor to descendant
else if (!fromType.IsValueType && !toType.IsValueType)
gen.EmitCast(toType);
else
{
var castOp = ctx.ResolveConvertorToType(fromType, toType);
if (castOp != null)
gen.EmitCall(castOp.MethodInfo);
else
error(fromType, toType);
}
}
else
error(fromType, toType);
}