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

            result = null;

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

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

                ReadOnlyCollection <ILocalVariable> variables = method.GetVariablesByName(name);
                if (variables.Count > 0)
                {
                    return(false);
                }
                //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())
                {
                    result = new EvaluatedExpression(name, name, null, field, declaringType.GetValue(field), false);
                    return(true);
                }

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

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

            // check the outer object?

            return(false);
            //throw new NotImplementedException();
        }
        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();
        }
        private EvaluatedExpression GetField(IReferenceType referenceType, string name)
        {
            Contract.Requires <ArgumentNullException>(referenceType != null, "referenceType");
            Contract.Requires <ArgumentNullException>(name != null, "name");
            Contract.Requires <ArgumentException>(!string.IsNullOrEmpty(name));

            IField field = referenceType.GetFieldByName(name);

            if (field != null)
            {
                string fullName = string.Format("{0}.{1}", referenceType.GetName(), name);
                if (field.GetIsStatic())
                {
                    return(new EvaluatedExpression(name, fullName, null, field, referenceType.GetValue(field), false));
                }

                throw new ArgumentException("The specified field is not static.");
            }

            throw new NotImplementedException();
        }
        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.");
        }