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?
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With