Last active
November 27, 2016 00:48
-
-
Save ortuagustin/80ccb3a39ed2a1625625dbba923fb80f to your computer and use it in GitHub Desktop.
ClassRtti
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.SysUtils, | |
System.Rtti; | |
type | |
TFoo<T: class> = class | |
public | |
constructor Create; | |
end; | |
TSomeClass = class | |
public | |
procedure FooMethod; | |
end; | |
constructor TFoo<T>.Create; | |
var | |
ctx: TRttiContext; | |
Each: TRttiMethod; | |
begin | |
inherited Create; | |
for Each in ctx.GetType(T).GetMethods do | |
begin | |
Writeln(Each.ToString); | |
end; | |
end; | |
procedure TSomeClass.FooMethod; | |
begin | |
Writeln('TSomeClass.FooMethod'); | |
end; | |
var | |
ClassFoo: TFoo<TSomeClass>; | |
begin | |
try | |
ClassFoo := TFoo<TSomeClass>.Create; | |
Readln; | |
except | |
on E: Exception do | |
Writeln(E.ClassName, ': ', E.Message); | |
end; | |
end. |
Thanks to your gist, I have my solution now :-)
Thanks a lot Agustin
I still don't get what is not working 😕
What's wrong with your last code? It compiled fine
What confuses me it's why are you using the generic if you are going to query an object. If you have the generic (which can be considered a parameter) why do you need the class in the constructor?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That is what I am trying to achieve: