public DelegateDetails Inspect(IClrCompositeObject delegateObj) { if (!delegateObj.Type.CanBeAssignedTo <Delegate>()) { throw new ArgumentException($"Not a delegate object wrapper: {delegateObj}"); } var target = delegateObj.GetFieldValue("_target"); var methodPtr = delegateObj.GetFieldValue <IntPtr>("_methodPtr"); var methodPtrAux = delegateObj.GetFieldValue <IntPtr>("_methodPtrAux"); if (methodPtrAux == IntPtr.Zero) { var method = LookupClrMethod(delegateObj, methodPtr); return(new DelegateDetails { Kind = DelegateKind.InstanceClosed1 | DelegateKind.StaticClosed4, TargetOrFirstArg = target, Method = method }); } var methodAux = LookupClrMethod(delegateObj, methodPtrAux); if (methodAux != null) { return(new DelegateDetails { Kind = DelegateKind.InstanceOpenNonVirtual2 | DelegateKind.StaticClosedSpecialSig5 | DelegateKind.StaticOpened6, Method = methodAux, Delegate = target, Thunk = methodPtr }); } if (delegateObj.Type.CanBeAssignedTo <MulticastDelegate>()) { var invocationCount = delegateObj.GetFieldValue <IntPtr>("_invocationCount"); var invocationList = delegateObj.GetFieldValue("_invocationList"); if (invocationList == null && invocationCount != IntPtr.Zero) { var method = delegateObj.Type.Heap.Runtime.GetMethodByHandle((ulong)invocationCount); return(new DelegateDetails { Kind = DelegateKind.InstanceOpenVirtual3, Method = method, Delegate = target, Thunk = methodPtr }); } } return(new DelegateDetails { Kind = DelegateKind.Secure7, Delegate = target, Thunk = methodPtr }); // See also: http://geekswithblogs.net/akraus1/archive/2012/05/20/149699.aspx }
public static Composite?ReadFrom(IClrCompositeObject obj) { if (obj == null) { return(null); } return(new Composite { StringField = obj.GetFieldValue <string>("StringField"), IntegerField = obj.GetFieldValue <int>("IntegerField") }); }
public static T GetFieldValue <T>(this IClrCompositeObject instance, string fieldName) { var value = instance.GetFieldValue(fieldName); if (Equals(default(T), value)) { return(default(T)); } if (TryCastAs(value, out T result)) { return(result); } throw new InvalidCastException($"Field '{fieldName}' of {instance} yields a {value.GetType().Name} and cannot be accessed as a {typeof(T).Name}"); }
public static string DumpFields(this IClrCompositeObject instance) { return(String.Join( Environment.NewLine, instance.Type.Fields.Select(f => $"{f.Name} -> {instance.GetFieldValue(f.Name)}"))); }