private Stmt UnboxAny(TypeReference type) { var value = this.stack.Pop(); var expr = new ExprUnboxAny(this.ctx, value, type); return(this.SsaLocalAssignment(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 VisitUnboxAny(ExprUnboxAny e) { this.code.Append("unbox("); this.Visit(e.Expr); this.code.Append(")"); return(e); }
protected virtual ICode VisitUnboxAny(ExprUnboxAny e) { this.ThrowOnNoOverride(); var expr = (Expr)this.Visit(e.Expr); if (expr != e.Expr) { return(new ExprUnboxAny(e.Ctx, expr, e.Type)); } else { return(e); } }
protected override ICode VisitUnboxAny(ExprUnboxAny e) { throw new InvalidOperationException("This should never occur"); }