I’m using this block:
procedure ExecMethod(Target: TClass; const MethodName: string; const Args: array of TValue);
var
LContext: TRttiContext;
LType: TRttiType;
LMethod: TRttiMethod;
begin
LType := LContext.GetType(Target);
for LMethod in LType.GetMethods do
if (LMethod.Parent = LType) and (LMethod.Name = MethodName) then begin
LMethod.Invoke(Target.Create, Args);
break;
end;
end;
like this:
ExecMethod(TFuncClass, 'Test1', []);
ExecMethod(TFuncClass, 'Test2', ['hey']);
ExecMethod(TFuncClass, 'Test3', [100]);
on this class:
TFuncClass = class(TObject)
published
procedure Test1;
procedure Test2(const str: string);
procedure Test3(i: integer);
// there's more, each one with different prototype
end;
var
FuncClass: TFuncClass;
but then, i keep getting access violations… or invalid cast pointer class (or whatever)..
As I noted at the source of your code, it leaks memory because it creates instances of the given class without ever freeing them. That shouldn’t cause any immediate run-time errors, though, so it is not the cause of the problem at hand.
The question’s code generalizes the original code to work on any given class, and in so doing, becomes technically wrong. To see why, you need to understand how Delphi constructs objects from class references:
When you call a constructor on a class-reference variable (as in
Target.Create), the compiler uses the knowledge it as at compile time to decide which constructor to call. In this case, the target of the call is aTClass, and the only constructor the compiler knows is available for that type isTObject.Create, so that’s the constructor that’s called. IfTFuncClasshas some other constructor — even if it matches the zero-argument signature inherited fromTObject— it’s never called. The type of the created object will still appear asTFuncClass, though — theClassTypefunction will returnTFuncClass, and theisoperator will work as expected.When code calls the wrong constructor on a class, it ends up with some half-valid instance of the desired class. Having invalid instances could lead to all sorts of problems. I wouldn’t be surprised if that included access violations, invalid type casts, invalid results, or whatever.
The code shown in the question shouldn’t have the problem I’ve described, though, since
TFuncClasshas no new constructor. However, the given code is obviously incomplete, so maybe it’s been over-simplified for presentation here.You’d be much better off leaving it the responsibility of the caller to provide an instance to call methods on, like this:
Use that function like so:
Note that this is all assuming that the second parameter, the string name of the method, is actually provided by some variable whose value is unknown until run time. If you’re passing a string literal to
ExecMethod, then you should stop callingExecMethod, stop messing around with RTTI, and just call the desired method directly:FuncClass.Test2('hey').