// // // // // $Revision: 2287 $ // using System; using System.Collections.Generic; using Debugger.Wrappers.CorDebug; namespace Debugger { // This part of the class provides support for classes and structures public partial class Value { internal ICorDebugObjectValue CorObjectValue { get { if (IsObject) { return CorValue.CastTo(); } else { throw new DebuggerException("Value is not an object"); } } } // internal ICorDebugObjectValue CorObjectValue { // get { // if (IsNull) throw new GetValueException("Value is null"); // // if (this.Type.IsClass) { // return this.CorValue.CastTo().Dereference().CastTo(); // } // if (this.Type.IsValueType) { // if (this.CorValue.Is()) { // // Dereference and unbox // return this.CorValue.CastTo().Dereference().CastTo().Object; // } else { // return this.CorValue.CastTo(); // } // } // // throw new DebuggerException("Value is not an object"); // } // } /// Returns true if the value is a class or value type public bool IsObject { get { return !IsNull && (this.Type.IsClass || this.Type.IsValueType); } } /// /// Get a field or property of an object with a given name. /// public NamedValue GetMember(string name) { try { DebugType currentType = this.Type; while (currentType != null) { foreach(MemberInfo memberInfo in currentType.GetMember(name, BindingFlags.All)) { if (memberInfo is FieldInfo) { return ((FieldInfo)memberInfo).GetValue(this); } if (memberInfo is PropertyInfo) { return ((PropertyInfo)memberInfo).GetValue(this); } } currentType = currentType.BaseType; } } catch { } return null; } /// /// Get all fields and properties of an object. /// public NamedValueCollection GetMembers() { return GetMembers(null, BindingFlags.All); } /// /// Get fields and properties of an object which are defined by a given type. /// /// Limit to type, null for all types /// Get only members with certain flags public NamedValueCollection GetMembers(DebugType type, BindingFlags bindingFlags) { if (IsObject) { return new NamedValueCollection(GetObjectMembersEnum(type, bindingFlags)); } else { return NamedValueCollection.Empty; } } IEnumerable GetObjectMembersEnum(DebugType type, BindingFlags bindingFlags) { DebugType currentType = type ?? this.Type; while (currentType != null) { foreach(FieldInfo field in currentType.GetFields(bindingFlags)) { yield return field.GetValue(this); } foreach(PropertyInfo property in currentType.GetProperties(bindingFlags)) { yield return property.GetValue(this); } if (type == null) { currentType = currentType.BaseType; } else { yield break; } } } } }