private static bool TryGetCollectionSize(IObjectReference objectReference, out int size)
        {
            size = 0;

            IArrayReference arrayReference = objectReference as IArrayReference;

            if (arrayReference != null)
            {
                size = arrayReference.GetLength();
                return(true);
            }

            IClassType classType = objectReference.GetReferenceType() as IClassType;

            if (classType == null)
            {
                return(false);
            }

            ReadOnlyCollection <IInterfaceType> interfaces = classType.GetInterfaces(true);

            if (interfaces.Any(i => _collectionInterfaces.Contains(i.GetName())))
            {
                IMethod sizeMethod = classType.GetConcreteMethod("size", "()I");
                using (IStrongValueHandle <IValue> result = objectReference.InvokeMethod(null, sizeMethod, InvokeOptions.None))
                {
                    if (result == null)
                    {
                        return(false);
                    }

                    IIntegerValue integerValue = result.Value as IIntegerValue;
                    if (integerValue != null)
                    {
                        size = integerValue.GetValue();
                        return(true);
                    }
                }
            }

            return(false);
        }
        /// <summary>
        /// Gets the DEBUG_PROPERTY_INFO structure that describes a property.
        /// </summary>
        /// <param name="dwFields">[in] A combination of values from the DEBUGPROP_INFO_FLAGS enumeration that specifies which fields are to be filled out in the pPropertyInfo structure.</param>
        /// <param name="dwRadix">[in] Radix to be used in formatting any numerical information.</param>
        /// <param name="dwTimeout">[in] Specifies the maximum time, in milliseconds, to wait before returning from this method. Use INFINITE to wait indefinitely.</param>
        /// <param name="rgpArgs">[in, out] Reserved for future use; set to a null value.</param>
        /// <param name="dwArgCount">[in] Reserved for future use; set to zero.</param>
        /// <param name="pPropertyInfo">[out] A DEBUG_PROPERTY_INFO structure that is filled in with the description of the property.</param>
        /// <returns>If successful, returns S_OK; otherwise returns error code.</returns>
        public int GetPropertyInfo(enum_DEBUGPROP_INFO_FLAGS dwFields, uint dwRadix, uint dwTimeout, IDebugReference2[] rgpArgs, uint dwArgCount, DEBUG_PROPERTY_INFO[] pPropertyInfo)
        {
            if (pPropertyInfo == null)
            {
                throw new ArgumentNullException("pPropertyInfo");
            }
            if (pPropertyInfo.Length == 0)
            {
                throw new ArgumentException();
            }

            bool getFullName   = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_FULLNAME) != 0;
            bool getName       = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_NAME) != 0;
            bool getType       = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_TYPE) != 0;
            bool getValue      = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE) != 0;
            bool getAttributes = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_ATTRIB) != 0;
            bool getProperty   = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_PROP) != 0;

            bool useAutoExpandValue = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE_AUTOEXPAND) != 0;
            bool noFormatting       = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE_RAW) != 0;
            bool noToString         = (dwFields & enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE_NO_TOSTRING) != 0;

            if (getFullName)
            {
                pPropertyInfo[0].bstrFullName = _evaluatedExpression.FullName;
                pPropertyInfo[0].dwFields    |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_FULLNAME;
            }

            if (getName)
            {
                pPropertyInfo[0].bstrName  = _evaluatedExpression.Name;
                pPropertyInfo[0].dwFields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_NAME;
            }

            if (getType)
            {
                pPropertyInfo[0].bstrType = _evaluatedExpression.ValueType.GetName();
                if (_evaluatedExpression.Value != null && !_evaluatedExpression.Value.GetValueType().Equals(_evaluatedExpression.ValueType))
                {
                    pPropertyInfo[0].bstrType += " {" + _evaluatedExpression.Value.GetValueType().GetName() + "}";
                }

                pPropertyInfo[0].dwFields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_TYPE;
            }

            if (getValue)
            {
                if (_evaluatedExpression.Value == null)
                {
                    pPropertyInfo[0].bstrValue = "null";
                }
                if (_evaluatedExpression.Value is IVoidValue)
                {
                    pPropertyInfo[0].bstrValue = "The expression has been evaluated and has no value.";
                }
                else if (_evaluatedExpression.Value is IPrimitiveValue)
                {
                    IBooleanValue booleanValue = _evaluatedExpression.Value as IBooleanValue;
                    if (booleanValue != null)
                    {
                        pPropertyInfo[0].bstrValue = booleanValue.GetValue().ToString().ToLowerInvariant();
                        pPropertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_VALUE_BOOLEAN;
                        if (booleanValue.GetValue())
                        {
                            pPropertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_VALUE_BOOLEAN_TRUE;
                        }
                    }

                    IByteValue byteValue = _evaluatedExpression.Value as IByteValue;
                    if (byteValue != null)
                    {
                        pPropertyInfo[0].bstrValue = byteValue.GetValue().ToString();
                    }

                    ICharValue charValue = _evaluatedExpression.Value as ICharValue;
                    if (charValue != null)
                    {
                        pPropertyInfo[0].bstrValue = EscapeSpecialCharacters(charValue.GetValue().ToString(), 10, '\'');
                    }

                    IShortValue shortValue = _evaluatedExpression.Value as IShortValue;
                    if (shortValue != null)
                    {
                        pPropertyInfo[0].bstrValue = shortValue.GetValue().ToString();
                    }

                    IIntegerValue integerValue = _evaluatedExpression.Value as IIntegerValue;
                    if (integerValue != null)
                    {
                        pPropertyInfo[0].bstrValue = integerValue.GetValue().ToString();
                    }

                    ILongValue longValue = _evaluatedExpression.Value as ILongValue;
                    if (longValue != null)
                    {
                        pPropertyInfo[0].bstrValue = longValue.GetValue().ToString();
                    }

                    IFloatValue floatValue = _evaluatedExpression.Value as IFloatValue;
                    if (floatValue != null)
                    {
                        pPropertyInfo[0].bstrValue = floatValue.GetValue().ToString();
                    }

                    IDoubleValue doubleValue = _evaluatedExpression.Value as IDoubleValue;
                    if (doubleValue != null)
                    {
                        pPropertyInfo[0].bstrValue = doubleValue.GetValue().ToString();
                    }
                }
                else if (_evaluatedExpression.Value is IArrayReference)
                {
                    IArrayReference arrayReference = _evaluatedExpression.Value as IArrayReference;
                    int             length         = arrayReference.GetLength();
                    IArrayType      arrayType      = (IArrayType)arrayReference.GetReferenceType();
                    pPropertyInfo[0].bstrValue = string.Format("{{{0}[{1}]}}", arrayType.GetComponentTypeName(), length);
                    if (length > 0)
                    {
                        pPropertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_OBJ_IS_EXPANDABLE;
                    }
                }
                else if (_evaluatedExpression.Value is IObjectReference)
                {
                    IStringReference stringReference = _evaluatedExpression.Value as IStringReference;
                    if (stringReference != null)
                    {
                        pPropertyInfo[0].bstrValue = EscapeSpecialCharacters(stringReference.GetValue(), 120, '"');
                    }
                    else
                    {
                        IObjectReference objectReference = _evaluatedExpression.Value as IObjectReference;
                        if (objectReference != null)
                        {
                            int collectionSize;
                            if (TryGetCollectionSize(objectReference, out collectionSize))
                            {
                                pPropertyInfo[0].bstrValue = string.Format("{{size() = {0}}}", collectionSize);
                                pPropertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_OBJ_IS_EXPANDABLE;
                            }
                            else
                            {
                                string     displayValue = null;
                                IClassType classType    = objectReference.GetReferenceType() as IClassType;
                                if (noToString || classType == null)
                                {
                                    displayValue = objectReference.GetReferenceType().GetName();
                                }
                                else
                                {
                                    IMethod method = classType.GetConcreteMethod("toString", "()Ljava/lang/String;");
                                    using (IStrongValueHandle <IValue> result = objectReference.InvokeMethod(null, method, InvokeOptions.None))
                                    {
                                        if (result != null)
                                        {
                                            stringReference = result.Value as IStringReference;
                                            if (stringReference != null)
                                            {
                                                displayValue = stringReference.GetValue();
                                            }
                                        }
                                    }

                                    if (displayValue == null)
                                    {
                                        IClassType objectClass = classType;
                                        while (true)
                                        {
                                            IClassType parentClass = objectClass.GetSuperclass();
                                            if (parentClass != null)
                                            {
                                                objectClass = parentClass;
                                            }
                                            else
                                            {
                                                break;
                                            }
                                        }

                                        IMethod objectToStringMethod = objectClass.GetConcreteMethod("toString", "()Ljava/lang/String;");

                                        // fall back to a non-virtual call
                                        using (IStrongValueHandle <IValue> result = objectReference.InvokeMethod(null, objectToStringMethod, InvokeOptions.NonVirtual | InvokeOptions.SingleThreaded))
                                        {
                                            if (result != null)
                                            {
                                                stringReference = result.Value as IStringReference;
                                                if (stringReference != null)
                                                {
                                                    displayValue = stringReference.GetValue();
                                                }
                                            }
                                        }
                                    }
                                }

                                pPropertyInfo[0].bstrValue = "{" + displayValue + "}";
                                pPropertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_OBJ_IS_EXPANDABLE;
                            }
                        }
                        else
                        {
                            pPropertyInfo[0].bstrValue = "Unrecognized value";
                            pPropertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_VALUE_ERROR;
                        }
                    }
                }

                pPropertyInfo[0].dwFields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE;
            }

            if (getAttributes)
            {
                if (_evaluatedExpression.Field != null)
                {
                    if (_evaluatedExpression.Field.GetIsPrivate())
                    {
                        pPropertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_ACCESS_PRIVATE;
                    }
                    if (_evaluatedExpression.Field.GetIsProtected())
                    {
                        pPropertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_ACCESS_PROTECTED;
                    }
                    if (_evaluatedExpression.Field.GetIsPublic())
                    {
                        pPropertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_ACCESS_PUBLIC;
                    }
                }

                pPropertyInfo[0].dwAttrib |= enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_VALUE_READONLY;
                pPropertyInfo[0].dwFields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_ATTRIB;
#if false
                bool expandable;
                bool hasId;
                bool canHaveId;
                bool readOnly;
                bool error;
                bool sideEffect;
                bool overloadedContainer;
                bool boolean;
                bool booleanTrue;
                bool invalid;
                bool notAThing;
                bool autoExpanded;
                bool timeout;
                bool rawString;
                bool customViewer;

                bool accessNone;
                bool accessPrivate;
                bool accessProtected;
                bool accessPublic;

                bool storageNone;
                bool storageGlobal;
                bool storageStatic;
                bool storageRegister;

                bool noModifiers;
                bool @virtual;
                bool constant;
                bool synchronized;
                bool @volatile;

                bool dataField;
                bool method;
                bool property;
                bool @class;
                bool baseClass;
                bool @interface;
                bool innerClass;
                bool mostDerived;

                bool multiCustomViewers;
#endif
            }

            if (getProperty)
            {
                pPropertyInfo[0].pProperty = this;
                pPropertyInfo[0].dwFields |= enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_PROP;
            }

            return(VSConstants.S_OK);
        }
        private EvaluatedExpression GetField(EvaluatedExpression value, string name)
        {
            Contract.Requires <ArgumentNullException>(value != null, "value");
            Contract.Requires <ArgumentNullException>(name != null, "name");
            Contract.Requires <ArgumentException>(!string.IsNullOrEmpty(name));

            IArrayReference arrayValue = value.Value as IArrayReference;

            if (arrayValue != null)
            {
                if (name == "length")
                {
                    string fullName = string.Format("({0}).{1}", value.FullName, name);
                    return(new EvaluatedExpression(name, fullName, _stackFrame.GetVirtualMachine().GetMirrorOf(arrayValue.GetLength()), value.HasSideEffects));
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }

            IReferenceType declaringType = value.ValueType as IReferenceType;

            if (declaringType == null)
            {
                throw new InvalidOperationException();
            }

            IField field = declaringType.GetFieldByName(name);

            if (field != null)
            {
                string fullName = string.Format("({0}).{1}", value.FullName, name);
                if (field.GetIsStatic())
                {
                    return(new EvaluatedExpression(name, fullName, null, field, declaringType.GetValue(field), value.HasSideEffects));
                }
                else
                {
                    IObjectReference objectReference = value.Value as IObjectReference;
                    if (objectReference == null)
                    {
                        throw new InvalidOperationException("Evaluation failed (todo: distinguish between null pointer and instance field referenced as a static field).");
                    }

                    return(new EvaluatedExpression(name, fullName, objectReference, field, objectReference.GetValue(field), value.HasSideEffects));
                }
            }

            throw new NotImplementedException();
        }