Reputation: 281
I have made a unit that's very generic and settings objects in this are all TObjects i don't want to make uses of any units that's why i'm doing this. So my approach is to use RTTI to call everything. But now i face a problem where i can call all the functions and give arguments and everything but when the method is a class procedure/function i can't call it and it says invalid type cast.
I checked on embarcadero's website it says that when we call rtti.invoke on a classmethod we have to set the first argument in Args as a class reference. I tried that but it doesn't work. Take a look at my code:
function TSomething.ExecMethodAndRet(MethodName: string;
Args: array of TValue): TObjectList<TObject>;
var
R : TRttiContext;
T : TRttiType;
M : TRttiMethod;
lArgs : array of TValue;
i : integer;
begin
T := R.GetType(MainObj.ClassInfo);
for M in t.GetMethods do
if (m.Parent = t) and (UpperCase(m.Name) = UpperCase(MethodName))then
begin
if (m.IsClassMethod) then
begin
for I := 0 to Length(Args) do
lArgs := [args[i]];
lArgs := [MainObj] + lArgs;
result := M.Invoke(MainObj, Args).AsType<TObjectList<TObject>>; <- this will say invalid type cast
end
else
result := M.Invoke(MainObj, Args).AsType<TObjectList<TObject>>; <- this one works when it's not a classMethod that's why i made a condition
end;
end;
I don't know what i'm doing wrong. Maybe it's not possible to do it without knowing the type of the object. My Main Obj is a TObject that is of the required type and i can call the methods of it. But That class procedure is really giving me a hard time.
Someone knows how i could achieve this?
Upvotes: 2
Views: 1617