/RttiInvoke.dpr Secret
Created
July 18, 2018 18:57
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program Project1; | |
{$APPTYPE CONSOLE} | |
{$R *.res} | |
uses | |
System.Rtti, | |
System.SysUtils; | |
type | |
{$M+} | |
IFoo = interface | |
['{65C02E99-4136-4572-B2C7-365AEA174E6E}'] | |
function GetBar: String; | |
end; | |
{$M-} | |
{$RTTI EXPLICIT METHODS([vcPrivate, vcProtected, vcPublic, vcPublished])} | |
TFoo = class(TInterfacedObject, IFoo) | |
strict private | |
function GetBar: String; | |
end; | |
function TFoo.GetBar: String; | |
begin | |
Result := 'Bar'; | |
end; | |
var | |
LMethod: TRttiMethod; | |
LFoo: IFoo; | |
begin | |
LFoo := TFoo.Create; | |
LMethod := TRttiContext.Create.GetType(TFoo).GetMethod('GetBar'); | |
Writeln(LMethod.Invoke(LFoo as TObject, []).AsString); //Prints: Bar | |
LMethod := TRttiContext.Create.GetType(TypeInfo(IFoo)).GetMethod('GetBar'); | |
Writeln(LMethod.Invoke(LFoo as TObject, []).AsString); //Access Violation | |
ReadLn; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment