Exemplo n.º 1
0
        /// <summary>
        /// Retrieves a field from this value.
        /// </summary>
        /// <param name="name">The name of the field.</param>
        /// <returns>A ClrValue representing this field.</returns>
        public virtual ClrValue GetField(string name)
        {
            ClrElementType el = ElementType;

            if (ClrRuntime.IsPrimitive(el))
            {
                // Primitives only have one field, named m_value.
                if (name != "m_value")
                {
                    throw new ArgumentException(string.Format("Field '{0}' does not exist in type '{1}'.", name, Type.Name));
                }

                // Getting m_value is the same as this ClrValue...
                return(this);
            }

            if (ClrRuntime.IsObjectReference(el) || !Interior)
            {
                return(AsObject().GetField(name));
            }

            Debug.Assert(ClrRuntime.IsValueClass(el));

            ulong address = Address;

            if (address == 0)
            {
                throw new NullReferenceException();
            }

            ClrType          type  = Type;
            ClrInstanceField field = type.GetFieldByName(name);

            if (field == null)
            {
                throw new ArgumentException(string.Format("Field '{0}' does not exist in type '{1}'.", name, Type.Name));
            }

            ulong result = field.GetAddress(address, Interior);

            return(new ClrValueImpl(_runtime, result, field));
        }
Exemplo n.º 2
0
        /// <summary>
        /// ToString implementation.
        /// </summary>
        /// <returns>A string value of this type.</returns>
        public override string ToString()
        {
            ClrElementType element = ElementType;

            if (element == ClrElementType.String)
            {
                return(AsString());
            }

            if (ClrRuntime.IsObjectReference(element))
            {
                return(AsObject().Address.ToString("x"));
            }

            if (ClrRuntime.IsValueClass(ElementType))
            {
                return($"{Type.Name} @{Address:x}");
            }

            switch (element)
            {
            case ClrElementType.Boolean:
                return(AsBoolean().ToString());

            case ClrElementType.Int8:
            case ClrElementType.UInt8:
                return(AsByte().ToString());

            case ClrElementType.Char:
                return(AsChar().ToString());

            case ClrElementType.UInt16:
            case ClrElementType.Int16:
                return(AsInt16().ToString());

            case ClrElementType.Int32:
            case ClrElementType.UInt32:
                return(AsInt32().ToString());

            case ClrElementType.Int64:
            case ClrElementType.UInt64:
                return(AsInt64().ToString());

            case ClrElementType.Pointer:
            case ClrElementType.FunctionPointer:
            case ClrElementType.NativeInt:
                return(AsIntPtr().ToInt64().ToString("x"));

            case ClrElementType.NativeUInt:
                return(AsUIntPtr().ToUInt64().ToString("x"));

            case ClrElementType.Float:
                return(AsFloat().ToString());

            case ClrElementType.Double:
                return(AsDouble().ToString());

            default:
                throw new NotImplementedException();
            }
        }