Reputation: 674
I'm actually trying to use Rtti to implent a generic method invoker. It should work like this:
So I need the class reference in order to get its Rtti information and seek for the method I want to invoke.
Is there any way to do that without implementing a class reference list of the classes I want to be working with?
Upvotes: 5
Views: 3668
Reputation: 136391
To get the class reference using his name you must use the TRttiContext.FindType
function passing the Name of the class and the retrieve the instance using the AsInstance
property and then you can call the constructor of the class.
var
Instance : TRttiInstanceType;
ctx : TRttiContext;
mClass : TValue;
begin
ctx := TRttiContext.Create;
Instance := ctx.FindType(ClassName).AsInstance; //ClassName is something like 'Classes.TStringList';
mClass := Instance.GetMethod('Create').Invoke(Instance.MetaclassType,[]);
//do your stuff here
end;
Upvotes: 8