public override string CallToString(EvaluationContext ctx, object obj)
        {
            SoftEvaluationContext cx = (SoftEvaluationContext)ctx;

            if (obj is StringMirror)
            {
                return(((StringMirror)obj).Value);
            }
            else if (obj is EnumMirror)
            {
                EnumMirror eob = (EnumMirror)obj;
                return(eob.StringValue);
            }
            else if (obj is PrimitiveValue)
            {
                return(((PrimitiveValue)obj).Value.ToString());
            }
            else if ((obj is StructMirror) && ((StructMirror)obj).Type.IsPrimitive)
            {
                // Boxed primitive
                StructMirror sm = (StructMirror)obj;
                if (sm.Fields.Length > 0 && (sm.Fields[0] is PrimitiveValue))
                {
                    return(((PrimitiveValue)sm.Fields[0]).Value.ToString());
                }
            }
            else if (obj == null)
            {
                return(string.Empty);
            }
            else if ((obj is ObjectMirror) && cx.Options.AllowTargetInvoke)
            {
                ObjectMirror ob     = (ObjectMirror)obj;
                MethodMirror method = OverloadResolve(cx, "ToString", ob.Type, new TypeMirror[0], true, false, false);
                if (method != null && method.DeclaringType.FullName != "System.Object")
                {
                    StringMirror res = cx.RuntimeInvoke(method, obj, new Value[0]) as StringMirror;
                    return(res != null ? res.Value : string.Empty);
                }
            }
            else if ((obj is StructMirror) && cx.Options.AllowTargetInvoke)
            {
                StructMirror ob     = (StructMirror)obj;
                MethodMirror method = OverloadResolve(cx, "ToString", ob.Type, new TypeMirror[0], true, false, false);
                if (method != null && method.DeclaringType.FullName != "System.ValueType")
                {
                    StringMirror res = cx.RuntimeInvoke(method, obj, new Value[0]) as StringMirror;
                    return(res != null ? res.Value : string.Empty);
                }
            }
            return(GetDisplayTypeName(GetValueTypeName(ctx, obj)));
        }
        public override object ForceLoadType(EvaluationContext gctx, string typeName)
        {
            // Shortcut to avoid a target invoke in case the type is already loaded
            object t = GetType(gctx, typeName);

            if (t != null)
            {
                return(t);
            }

            SoftEvaluationContext ctx = (SoftEvaluationContext)gctx;

            if (!ctx.Options.AllowTargetInvoke)
            {
                return(null);
            }
            TypeMirror tm    = (TypeMirror)ctx.Thread.Type.GetTypeObject().Type;
            TypeMirror stype = ctx.Session.GetType("System.String");

            if (stype == null)
            {
                // If the string type is not loaded, we need to get it in another way
                StringMirror ss = ctx.Thread.Domain.CreateString("");
                stype = ss.Type;
            }
            TypeMirror[] ats = new TypeMirror[] { stype };
            MethodMirror met = OverloadResolve(ctx, "GetType", tm, ats, false, true, true);

            try {
                tm.InvokeMethod(ctx.Thread, met, new Value[] { (Value)CreateValue(ctx, typeName) });
            } catch {
                return(null);
            } finally {
                ctx.Session.StackVersion++;
            }
            return(GetType(ctx, typeName));
        }
Exemplo n.º 3
0
 public StringAdaptor(StringMirror str)
 {
     atleast_2_10 = str.VirtualMachine.Version.AtLeast(2, 10);
     this.str     = str;
 }