Joh*_*ohn 2 delphi casting rtti delphi-10.1-berlin
我制作了一个非常通用的单元,其中的设置对象都是 TObject,我不想使用任何单元,这就是我这样做的原因。所以我的方法是使用RTTI来调用一切。但是现在我面临一个问题,我可以调用所有函数并提供参数和所有内容,但是当该方法是类过程/函数时,我无法调用它并且它说无效类型转换。
我查看了 embarcadero 的网站,它说当我们在 classmethod 上调用 rtti.invoke 时,我们必须将 Args 中的第一个参数设置为类引用。我试过了,但它不起作用。看看我的代码:
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;
Run Code Online (Sandbox Code Playgroud)
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?
| 归档时间: |
|
| 查看次数: |
852 次 |
| 最近记录: |