Given only a TRttiMethod
, is it possible to find the defining class?
I have a class procedure defined in a base class, which is showing up when I search for the methods on the derived class. I can call it, however I want to find out the name of the base class in which the method is actually defined, and not the name of the derived class.
All properties on TRttiMethod
regarding classtype, etc all return the TRttiMethod
's own definition and not of the implementing class.
TBase = class
public
class procedure Write;
end;
THigher = class(TBase);
var Context:=TRttiContext.Create;
var T:=Context.GetType(THigher);
var Method: TRttiMethod:=nil;
for var Item in T.GetMethods('Write') do
if (Item.MethodKind=TMethodKind.mkClassProcedure) then
begin
Method:=Item;
// How to get TBase from Method ?
break;
end;
The TRttiMethod.Parent
property is a TRttiType
representing the declaring class.
In your example, the Parent
refers to the TBase
class. The Parent.Name
property, and the Parent.ToString()
method, will return the string 'TBase'
.
If you need an actual TClass
for the method's declaring class, you can get it from the Parent.AsInstance.MetaclassType
property.