private EvaluatedExpression GetValueInScope(string name)
        {
            Contract.Requires <ArgumentNullException>(name != null, "name");
            Contract.Requires <ArgumentException>(!string.IsNullOrEmpty(name));

            IMethod method = _stackFrame.GetLocation().GetMethod();

            // check the stack frame
            if (_stackFrame.GetHasVariableInfo())
            {
                ILocalVariable variable = _stackFrame.GetVisibleVariableByName(name);
                if (variable != null)
                {
                    return(new EvaluatedExpression(name, name, variable, _stackFrame.GetValue(variable), false));
                }

                ReadOnlyCollection <ILocalVariable> variables = method.GetVariablesByName(name);
                if (variables.Count > 0)
                {
                    throw new InvalidOperationException("Evaluation failed because the variable is not visible in the current scope.");
                }
            }

            // check the enclosing object for a visible field
            IReferenceType declaringType = method.GetDeclaringType();
            IField         field         = declaringType.GetFieldByName(name);

            if (field != null)
            {
                if (field.GetIsStatic())
                {
                    return(new EvaluatedExpression(name, name, null, field, declaringType.GetValue(field), false));
                }

                if (method.GetIsStatic())
                {
                    throw new InvalidOperationException("The instance field cannot be accessed from a static method.");
                }

                return(new EvaluatedExpression(name, name, _stackFrame.GetThisObject(), field, _stackFrame.GetThisObject().GetValue(field), false));
            }

            // check the outer object
            IField           outerClassField     = declaringType.GetFieldByName("this$0");
            IObjectReference nestedClassInstance = null;

            while (outerClassField != null)
            {
                if (nestedClassInstance == null)
                {
                    nestedClassInstance = _stackFrame.GetThisObject();
                }
                else
                {
                    nestedClassInstance = nestedClassInstance.GetValue(outerClassField) as IObjectReference;
                }

                if (nestedClassInstance == null)
                {
                    break;
                }

                // what if this$0 is defined in a superclass?
                IClassType fieldType = outerClassField.GetFieldType() as IClassType;
                field = fieldType.GetFieldByName(name);
                if (field != null)
                {
                    if (field.GetIsStatic())
                    {
                        return(new EvaluatedExpression(name, name, null, field, fieldType.GetValue(field), false));
                    }

                    if (method.GetIsStatic())
                    {
                        throw new InvalidOperationException("The instance field cannot be accessed from a static method.");
                    }

                    return(new EvaluatedExpression(name, name, nestedClassInstance, field, nestedClassInstance.GetValue(field), false));
                }

                outerClassField = fieldType.GetFieldByName("this$0");
            }

            throw new InvalidOperationException("No member by this name is available in the current scope.");
        }