public static bool AreTypesEquivalent(IClrObject self, IClrObject other) { if (self.GetType() != other.GetType()) { return(false); } return(ClrTypeEqualityComparer.Instance.Equals(self.Type, other.Type)); }
public static T CastAs <T>(this IClrObject obj) { if (Equals(default(T), obj)) { return(default(T)); } if (TryCastAs(obj, out T result)) { return(result); } throw new InvalidCastException($"This object is a {obj.GetType().Name} and cannot be accessed as a {typeof(T).Name}"); }
/// <summary> /// Two objects at the same address in the same heap should be of the same type. /// </summary> /// <param name="self"></param> /// <param name="other"></param> /// <returns></returns> public static bool AssertTypesEquivalent(IClrObject self, IClrObject other) { if (!ReferenceEquals(self.Type.Heap, other.Type.Heap)) { return(false); } Debug.Assert(ClrTypeEqualityComparer.Instance.Equals(self.Type, other.Type), "Mismatched ClrTypes at the same address.", "This: {0}\nOther: {1}", self.Type, other.Type); if (self.GetType() == other.GetType()) { return(true); } Debug.Fail("Mismatched IClrObject types at the same address.", $"This: {self.GetType()}\nOther: {other.GetType()}"); return(false); }