protected override ICode VisitCast(ExprCast e) {
     var ctx = e.Ctx;
     var mCast = ctx.Module.Import(((Func<object, Type, object>)InternalFunctions.Cast).Method);
     var eType = new ExprJsTypeVarName(ctx, e.Type);
     var expr = new ExprCall(ctx, e.Type, mCast, null, e.Expr, eType);
     return expr;
 }
 protected override ICode VisitUnboxAny(ExprUnboxAny e) {
     if (!e.Type.IsValueType) {
         // On ref-type, unbox-any becomes a castclass
         var expr = (Expr)this.Visit(e.Expr);
         var cast = new ExprCast(e.Ctx, expr, e.Type);
         return cast;
     }
     var ctx = e.Ctx;
     if (e.Type.IsNullable()) {
         // If obj==null create Nullable with hasValue=false
         // If obj.Type not assignable to e.InnerType throw InvalidCastEx
         var innerType = e.Type.GetNullableInnerType();
         var unboxMethod = ((Func<object, int?>)InternalFunctions.UnboxAnyNullable<int>).Method.GetGenericMethodDefinition();
         var mUnbox = ctx.Module.Import(unboxMethod).MakeGeneric(innerType);
         var unboxAnyCall = new ExprCall(ctx, mUnbox, null, e.Expr);
         return unboxAnyCall;
     } else {
         // If obj==null throw NullRefEx
         // If obj.Type not assignable to e.Type throw InvalidCastEx
         // otherwise unbox
         var unboxMethod = ((Func<object, int>)InternalFunctions.UnboxAnyNonNullable<int>).Method.GetGenericMethodDefinition();
         var mUnbox = ctx.Module.Import(unboxMethod).MakeGeneric(e.Type);
         var unboxAnyCall = new ExprCall(ctx, mUnbox, null, e.Expr);
         return unboxAnyCall;
     }
 }
 protected override ICode VisitCast(ExprCast e) {
     this.code.AppendFormat("({0})", e.Type);
     this.Visit(e.Expr);
     return e;
 }
示例#4
0
 protected override ICode VisitCast(ExprCast e) {
     throw new InvalidOperationException("This should never occur");
 }
 private Stmt Cast(TypeReference toType) {
     var expr = this.stack.Pop();
     var cast = new ExprCast(this.ctx, expr, toType);
     return this.SsaLocalAssignment(cast);
 }