1

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)..

  • 1
    It's not clear what you need. Do you just not know how to call a class method? Or do you need to call that method from within the method of a different class? – itsbruce Nov 06 '12 at 14:24
  • 1
    The code in the question does what you describe. – David Heffernan Nov 06 '12 at 15:17
  • 1
    What does the title of this question have to do with its body? – Rob Kennedy Nov 06 '12 at 16:10
  • I'm sorry if i made it hard to understand me, let me rephrase what i meant... i want ExecMethod to be a part of a CLASS and that it will execute methods within the SAME CLASS it belongs to.. without having to TARGET it on the class... ;] –  Nov 06 '12 at 17:21
  • `ExecMethod` is *already* a part of a class. Specifically, `TMyClass`. And the given code only executes methods from the same class. Maybe you could post an example of some code that you *wish* would work. – Rob Kennedy Nov 06 '12 at 17:31
  • 2
    You say you get two entirely different kinds of errors, "or whatever." Please be precise. Copy and paste the *actual* error text you get. Saying "or whatever" tells me you can't be bothered to pay attention to the problem. If you can't be bothered, why should anyone else? – Rob Kennedy Nov 06 '12 at 21:52
  • when calling it WITH arguments, i get 2 access violations... when calling Test1 (which does not accept arguments) no exceptions, just bad results. –  Nov 06 '12 at 22:05

1 Answers1

2

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 a TClass, and the only constructor the compiler knows is available for that type is TObject.Create, so that's the constructor that's called. If TFuncClass has some other constructor — even if it matches the zero-argument signature inherited from TObject — it's never called. The type of the created object will still appear as TFuncClass, though — the ClassType function will return TFuncClass, and the is operator 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 TFuncClass has 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:

procedure ExecMethod(Target: TObject; const MethodName: string; const Args: array of TValue);
var
  LContext: TRttiContext;
  LType: TRttiType;
  LMethod: TRttiMethod;
begin
  LType := LContext.GetType(Target.ClassType);
  for LMethod in LType.GetMethods(MethodName) do
    // TODO: Beware of overloaded methods
    LMethod.Invoke(Target, Args);
end;

Use that function like so:

FuncClass := TFuncClass.Create(...);
try
  ExecMethod(FuncClass, 'Test1', []);
  ExecMethod(FuncClass, 'Test2', ['hey']);
  ExecMethod(FuncClass, 'Test3', [100]);
finally
  FuncClass.Free
end;

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 calling ExecMethod, stop messing around with RTTI, and just call the desired method directly: FuncClass.Test2('hey').

Community
  • 1
  • 1
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • I tried your code like this:
    type MyClass = class
      published
        procedure TestFunc;
    
    ExecMethod(MyClass, 'TestFunc', []);
    
    gives exception..
    –  Nov 06 '12 at 17:28
  • And of course it didn't compile, right? That's because `MyClass` in that example is not a value of type `TObject`. `TestFunc` is an instance method. It requires you to have an instance of `MyClass`. RTTI doesn't let you avoid that requirement. Do you want `TestFunc` to be a *class* method instead of an instance method? Do you want it to be a *static* method? Please remember that nobody can read your mind. You need to *ask* for what you want. – Rob Kennedy Nov 06 '12 at 17:33
  • i tried your code.. it gives me exception. i replaced TObject with TClass... but i get error still. –  Nov 06 '12 at 17:38
  • Of course you did. I already explained why: You can't call instance methods without an instance. The RTL is correct to throw an exception when you attempt to violate the rules. – Rob Kennedy Nov 06 '12 at 17:41
  • what do you mean "without an instance" ? –  Nov 06 '12 at 17:41
  • An instance is an object of a particular type. You've defined a class, `MyClass`. When you run `foo := MyClass.Create`, the result is that `foo` holds a reference to an *instance* of `MyClass`. That's the thing you typically call methods on: `foo.TestFunc`. There are also *class methods*, which do not require an instance; you can call them directly on the *class reference*. If `TestFunc` were a class method, you could call `MyClass.TestFunc`. That's essentially what you're *trying* to do with your modified version of my code, but `TestFunc` isn't a class method, so the RTL balks. – Rob Kennedy Nov 06 '12 at 17:48
  • Ny main goal is this: i have several procedures in a class, each one has a different prototype, some don't have... i want the ExecMethod to work with all of them, and accept DIFFERENT arguments each time –  Nov 06 '12 at 19:11
  • 1
    `ExecMethod` already works with all of them. As I suggested before, maybe you could post some code that *demonstrates* the problem you're facing. You can click the "edit" link beneath your question and add to it; the question space offers better formatting than these comments. It's OK if the code you post doesn't work. You're obviously having trouble explaining your task in words, so maybe explaining it in code would be easier for you. – Rob Kennedy Nov 06 '12 at 19:16
  • Please please please don't dribble this info out in a long comment trail. Add the pertinent information to the question. – David Heffernan Nov 06 '12 at 19:30
  • 1
    I've now noticed that [the code you started with](http://stackoverflow.com/revisions/13252701/1) never actually matched [what you claimed it came from](http://stackoverflow.com/a/4189282/33732). You added it to a class and introduced errors. I didn't check the source because I assumed you were posting something known to be good. You've been exceptionally difficult to work with today. – Rob Kennedy Nov 06 '12 at 22:22
  • I was being wrong all this time, lol... the reason it never worked (i didn't even mention this) is because i transmmit a record over a socket (which has 2 members, method name in static array and a dynamic array which is an array of TValue <--- the problem), and the invokation takes place at client, and client receives a flawed record -_- –  Nov 06 '12 at 22:51