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)));
        }
        T BuildAttribute <T> (CustomAttributeDataMirror attr)
        {
            List <object> args = new List <object> ();

            foreach (CustomAttributeTypedArgumentMirror arg in attr.ConstructorArguments)
            {
                object val = arg.Value;
                if (val is TypeMirror)
                {
                    // The debugger attributes that take a type as parameter of the constructor have
                    // a corresponding constructor overload that takes a type name. We'll use that
                    // constructor because we can't load target types in the debugger process.
                    // So what we do here is convert the Type to a String.
                    TypeMirror tm = (TypeMirror)val;
                    val = tm.FullName + ", " + tm.Assembly.ManifestModule.Name;
                }
                else if (val is EnumMirror)
                {
                    EnumMirror em = (EnumMirror)val;
                    val = em.Value;
                }
                args.Add(val);
            }
            Type   type = typeof(T);
            object at   = Activator.CreateInstance(type, args.ToArray());

            foreach (CustomAttributeNamedArgumentMirror arg in attr.NamedArguments)
            {
                object val     = arg.TypedValue.Value;
                string postFix = "";
                if (arg.TypedValue.ArgumentType == typeof(Type))
                {
                    postFix = "TypeName";
                }
                if (arg.Field != null)
                {
                    type.GetField(arg.Field.Name + postFix).SetValue(at, val);
                }
                else if (arg.Property != null)
                {
                    type.GetProperty(arg.Property.Name + postFix).SetValue(at, val, null);
                }
            }
            return((T)at);
        }
        public override object TryCast(EvaluationContext ctx, object obj, object targetType)
        {
            SoftEvaluationContext cx = (SoftEvaluationContext)ctx;

            if (obj == null)
            {
                return(null);
            }
            object valueType = GetValueType(ctx, obj);

            if (valueType is TypeMirror)
            {
                if ((targetType is TypeMirror) && ((TypeMirror)targetType).IsAssignableFrom((TypeMirror)valueType))
                {
                    return(obj);
                }
                // Try casting the primitive type of the enum
                EnumMirror em = obj as EnumMirror;
                if (em != null)
                {
                    return(TryCast(ctx, CreateValue(ctx, em.Value), targetType));
                }
            }
            else if (valueType is Type)
            {
                if (targetType is TypeMirror)
                {
                    TypeMirror tm = (TypeMirror)targetType;
                    if (tm.IsEnum)
                    {
                        PrimitiveValue casted = TryCast(ctx, obj, tm.EnumUnderlyingType) as PrimitiveValue;
                        if (casted == null)
                        {
                            return(null);
                        }
                        return(cx.Session.VirtualMachine.CreateEnumMirror(tm, casted));
                    }
                    targetType = Type.GetType(((TypeMirror)targetType).FullName, false);
                }
                Type tt = targetType as Type;
                if (tt != null)
                {
                    if (tt.IsAssignableFrom((Type)valueType))
                    {
                        return(obj);
                    }
                    if (obj is PrimitiveValue)
                    {
                        obj = ((PrimitiveValue)obj).Value;
                    }
                    if (tt != typeof(string) && !(obj is string))
                    {
                        try {
                            if (obj == null)
                            {
                                return(null);
                            }
                            object res = System.Convert.ChangeType(obj, tt);
                            return(CreateValue(ctx, res));
                        } catch {
                        }
                    }
                }
            }
            return(null);
        }