From 9a645660c784a564f3a4cc63d62731feb771a4e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Srbeck=C3=BD?= Date: Tue, 1 Jan 2008 13:37:13 +0000 Subject: [PATCH] Moved code to the Value class: - Value.GetFieldValue - Value.GetPropertyValue - Value.SetPropertyValue - Value.InvokeMethod - Value.AsyncInvokeMethod git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2769 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Project/Src/Variables/Types/FieldInfo.cs | 44 ----- .../Project/Src/Variables/Types/MethodInfo.cs | 24 --- .../Src/Variables/Types/PropertyInfo.cs | 62 +------ .../Src/Variables/Values/Value.Object.cs | 170 +++++++++++++++++- 4 files changed, 174 insertions(+), 126 deletions(-) diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Types/FieldInfo.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Types/FieldInfo.cs index c9f0c272ed..d2d36b61e6 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Types/FieldInfo.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Types/FieldInfo.cs @@ -68,49 +68,5 @@ namespace Debugger { this.fieldProps = fieldProps; } - - /// - /// Given an object of correct type, get the value of this field - /// - public Value GetValue(Value objectInstance) { - return new Value( - this.Process, - this.Name, - new Ast.FieldReferenceExpression( - new Ast.IdentifierExpression("parent"), // TODO - this - ), - new IExpirable[] {objectInstance}, - new IMutable[] {objectInstance}, - delegate { return GetCorValue(objectInstance); } - ); - } - - ICorDebugValue GetCorValue(Value objectInstance) - { - if (!DeclaringType.IsInstanceOfType(objectInstance)) { - throw new CannotGetValueException("Object is not of type " + DeclaringType.FullName); - } - - // Current frame is used to resolve context specific static values (eg. ThreadStatic) - ICorDebugFrame curFrame = null; - if (this.Process.IsPaused && - this.Process.SelectedThread != null && - this.Process.SelectedThread.LastFunction != null && - this.Process.SelectedThread.LastFunction.CorILFrame != null) { - - curFrame = this.Process.SelectedThread.LastFunction.CorILFrame.CastTo(); - } - - try { - if (this.IsStatic) { - return DeclaringType.CorType.GetStaticFieldValue(MetadataToken, curFrame); - } else { - return objectInstance.CorObjectValue.GetFieldValue(DeclaringType.CorType.Class, MetadataToken); - } - } catch { - throw new CannotGetValueException("Can not get value of field"); - } - } } } diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Types/MethodInfo.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Types/MethodInfo.cs index db33ba26d8..05d14176dc 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Types/MethodInfo.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Types/MethodInfo.cs @@ -99,29 +99,5 @@ namespace Debugger } throw new DebuggerException("Not found"); } - - /// - /// Synchronously invoke the method of an a given object - /// - public Value Invoke(Value objectInstance, Value[] arguments) - { - return Eval.InvokeMethod( - this, - this.IsStatic ? null : objectInstance, - arguments ?? new Value[0] - ); - } - - /// - /// Asynchronously invoke the method of an a given object - /// - public Eval AsyncInvoke(Value objectInstance, Value[] arguments) - { - return Eval.AsyncInvokeMethod( - this, - this.IsStatic ? null : objectInstance, - arguments ?? new Value[0] - ); - } } } diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Types/PropertyInfo.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Types/PropertyInfo.cs index 16ecc74620..fbeb0b464e 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Types/PropertyInfo.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Types/PropertyInfo.cs @@ -68,63 +68,17 @@ namespace Debugger } /// Get the get accessor of the property - public MethodInfo GetGetMethod() - { - return getMethod; + public MethodInfo GetMethod { + get { + return getMethod; + } } /// Get the set accessor of the property - public MethodInfo GetSetMethod() - { - return setMethod; - } - - /// Get the value of the property using the get accessor - public Value GetValue(Value objectInstance) - { - return GetValue(objectInstance, null); - } - - /// Get the value of indexer property - public Value GetValue(Value objectInstance, Value[] parameters) - { - if (getMethod == null) throw new CannotGetValueException("Property does not have a get method"); - parameters = parameters ?? new Value[0]; - - List dependencies = new List(); - dependencies.Add(objectInstance); - dependencies.AddRange(parameters); - - return new Value( - this.Process, - this.Name, - new Ast.PropertyReferenceExpression( - new Ast.IdentifierExpression("parent"), // TODO - this - ), - dependencies.ToArray(), - dependencies.ToArray(), - delegate { return getMethod.Invoke(objectInstance, parameters).RawCorValue; } - ); - } - - /// Set the value of the property using the set accessor - public Value SetValue(Value objectInstance, Value newValue) - { - return SetValue(objectInstance, newValue, null); - } - - /// Set the value of indexer property - public Value SetValue(Value objectInstance, Value newValue, Value[] parameters) - { - if (setMethod == null) throw new CannotGetValueException("Property does not have a set method"); - - parameters = parameters ?? new Value[0]; - Value[] allParams = new Value[1 + parameters.Length]; - allParams[0] = newValue; - parameters.CopyTo(allParams, 1); - - return setMethod.Invoke(objectInstance, allParams); + public MethodInfo SetMethod { + get { + return setMethod; + } } } } diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Values/Value.Object.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Values/Value.Object.cs index d3a7c271db..3d81c99b07 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Values/Value.Object.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Variables/Values/Value.Object.cs @@ -9,6 +9,8 @@ using System; using System.Collections.Generic; using Debugger.Wrappers.CorDebug; +using Ast = ICSharpCode.NRefactory.Ast; + namespace Debugger { // This part of the class provides support for classes and structures @@ -31,6 +33,166 @@ namespace Debugger } } + /// + /// Get the value of given field. + /// Field may be static + /// + public Value GetFieldValue(FieldInfo fieldInfo) + { + return Value.GetFieldValue(this, fieldInfo); + } + + /// + /// Get the value of given field. + /// objectInstance must not be null. + /// Field may be static + /// + public static Value GetFieldValue(Value objectInstance, FieldInfo fieldInfo) + { + return new Value( + objectInstance.Process, + fieldInfo.Name, + new Ast.FieldReferenceExpression( + new Ast.IdentifierExpression("parent"), // TODO + fieldInfo + ), + new IExpirable[] {objectInstance}, + new IMutable[] {objectInstance}, + delegate { return GetFieldCorValue(objectInstance, fieldInfo); } + ); + } + + static ICorDebugValue GetFieldCorValue(Value objectInstance, FieldInfo fieldInfo) + { + if (!fieldInfo.DeclaringType.IsInstanceOfType(objectInstance)) { + throw new CannotGetValueException("Object is not of type " + fieldInfo.DeclaringType.FullName); + } + + // Current frame is used to resolve context specific static values (eg. ThreadStatic) + ICorDebugFrame curFrame = null; + if (objectInstance.Process.IsPaused && + objectInstance.Process.SelectedThread != null && + objectInstance.Process.SelectedThread.LastFunction != null && + objectInstance.Process.SelectedThread.LastFunction.CorILFrame != null) { + + curFrame = objectInstance.Process.SelectedThread.LastFunction.CorILFrame.CastTo(); + } + + try { + if (fieldInfo.IsStatic) { + return fieldInfo.DeclaringType.CorType.GetStaticFieldValue(fieldInfo.MetadataToken, curFrame); + } else { + return objectInstance.CorObjectValue.GetFieldValue(fieldInfo.DeclaringType.CorType.Class, fieldInfo.MetadataToken); + } + } catch { + throw new CannotGetValueException("Can not get value of field"); + } + } + + /// Get the value of the property using the get accessor + public Value GetPropertyValue(PropertyInfo propertyInfo) + { + return GetPropertyValue(this, propertyInfo, null); + } + + /// Get the value of the property using the get accessor + public Value GetPropertyValue(PropertyInfo propertyInfo, Value[] arguments) + { + return GetPropertyValue(this, propertyInfo, arguments); + } + + /// Get the value of the property using the get accessor + public static Value GetPropertyValue(Value objectInstance, PropertyInfo propertyInfo) + { + return GetPropertyValue(objectInstance, propertyInfo, null); + } + + /// Get the value of the property using the get accessor + public static Value GetPropertyValue(Value objectInstance, PropertyInfo propertyInfo, Value[] arguments) + { + if (propertyInfo.GetMethod == null) throw new CannotGetValueException("Property does not have a get method"); + arguments = arguments ?? new Value[0]; + + List dependencies = new List(); + dependencies.Add(objectInstance); + dependencies.AddRange(arguments); + + return new Value( + objectInstance.Process, + propertyInfo.Name, + new Ast.PropertyReferenceExpression( + new Ast.IdentifierExpression("parent"), // TODO + propertyInfo + ), + dependencies.ToArray(), + dependencies.ToArray(), + delegate { return Value.InvokeMethod(objectInstance, propertyInfo.GetMethod, arguments).RawCorValue; } + ); + } + + /// Set the value of the property using the set accessor + public Value SetPropertyValue(PropertyInfo propertyInfo, Value newValue) + { + return SetPropertyValue(this, propertyInfo, null, newValue); + } + + /// Set the value of the property using the set accessor + public Value SetPropertyValue(PropertyInfo propertyInfo, Value[] arguments, Value newValue) + { + return SetPropertyValue(this, propertyInfo, arguments, newValue); + } + + /// Set the value of the property using the set accessor + public static Value SetPropertyValue(Value objectInstance, PropertyInfo propertyInfo, Value newValue) + { + return SetPropertyValue(objectInstance, propertyInfo, null, newValue); + } + + /// Set the value of the property using the set accessor + public static Value SetPropertyValue(Value objectInstance, PropertyInfo propertyInfo, Value[] arguments, Value newValue) + { + if (propertyInfo.SetMethod == null) throw new CannotGetValueException("Property does not have a set method"); + + arguments = arguments ?? new Value[0]; + Value[] allParams = new Value[1 + arguments.Length]; + allParams[0] = newValue; + arguments.CopyTo(allParams, 1); + + return Value.InvokeMethod(objectInstance, propertyInfo.SetMethod, allParams); + } + + /// Synchronously invoke the method + public Value InvokeMethod(MethodInfo methodInfo, params Value[] arguments) + { + return InvokeMethod(this, methodInfo, arguments); + } + + /// Synchronously invoke the method + public static Value InvokeMethod(Value objectInstance, MethodInfo methodInfo, params Value[] arguments) + { + return Eval.InvokeMethod( + methodInfo, + methodInfo.IsStatic ? null : objectInstance, + arguments ?? new Value[0] + ); + } + + /// Asynchronously invoke the method + public Eval AsyncInvokeMethod(MethodInfo methodInfo, params Value[] arguments) + { + return AsyncInvokeMethod(this, methodInfo, arguments); + } + + /// Asynchronously invoke the method + public static Eval AsyncInvokeMethod(Value objectInstance, MethodInfo methodInfo, params Value[] arguments) + { + return Eval.AsyncInvokeMethod( + methodInfo, + methodInfo.IsStatic ? null : objectInstance, + arguments ?? new Value[0] + ); + } + /// /// Get a field or property of an object with a given name. /// @@ -40,10 +202,10 @@ namespace Debugger while (currentType != null) { foreach(MemberInfo memberInfo in currentType.GetMember(name, BindingFlags.All)) { if (memberInfo is FieldInfo) { - return ((FieldInfo)memberInfo).GetValue(this); + return this.GetFieldValue((FieldInfo)memberInfo); } if (memberInfo is PropertyInfo) { - return ((PropertyInfo)memberInfo).GetValue(this); + return this.GetPropertyValue((PropertyInfo)memberInfo); } } currentType = currentType.BaseType; @@ -78,10 +240,10 @@ namespace Debugger DebugType currentType = type ?? this.Type; while (currentType != null) { foreach(FieldInfo field in currentType.GetFields(bindingFlags)) { - yield return field.GetValue(this); + yield return this.GetFieldValue(field); } foreach(PropertyInfo property in currentType.GetProperties(bindingFlags)) { - yield return property.GetValue(this); + yield return this.GetPropertyValue(property); } if (type == null) { currentType = currentType.BaseType;