System.TObject.MethodAddress
Delphi
class function MethodAddress(const Name: _ShortStr): Pointer; overload;
class function MethodAddress(const Name: string): Pointer; overload;
C++
__classmethod void * __fastcall MethodAddress(const ShortString &Name)/* overload */;
__classmethod void * __fastcall MethodAddress(const UnicodeString Name)/* overload */;
Properties
Type | Visibility | Source | Unit | Parent |
---|---|---|---|---|
function | public | System.pas systobj.h |
System | TObject |
Description
Returns the address of a class method by name.
Note: You can use MethodAddress for published methods only.
There are situations when it is useful to invoke an object method without hard coding the method name in advance. Call MethodAddress to dynamically retrieve the address of such a method by specifying the method Name as a string.
An easy way to invoke the method is to define a procedure or function data type, such as:
type TProc = procedure of object;
Assign the object name and the MethodAddress method to a TMethod variable, such as:
MethodVar.Data := Pointer(ObjectInstanceName); MethodVar.Code := ObjectInstanceName.MethodAddress('MethodNameString');
Pass this in a call to a variable of the procedure or function type:
Proc := TProc(MethodVar); Proc;