The following code should work:
procedure NilAllEventHandlers(myObject: TObject); var context: TRttiContext; rType: TRttiType; field: TRttiField; value: TValue; nilMethod: TMethod; begin nilMethod.Code := nil; nilMethod.Data := nil; context := TRttiContext.Create; rType := context.GetType(TButton); for field in rType.GetFields do begin if field.FieldType.TypeKind = tkMethod then begin TValue.Make(@nilMethod, field.FieldType.Handle, value); field.SetValue(myObject, value); end; end; end;
But this does not happen because in TValue.TryCast an error occurs when working with the value of TMethod, whose .Code parameter is nil . I will report this to QC. Hope it will be fixed in D2011 or updated. Until then, try the old style.
EDIT: displayed as QC # 81416 . Vote if you want this to be fixed.
source share