Can I pass a class type as a procedure parameter

I want to create a function that returns all the names of a particular class as a list of strings. Based on the previous solution / question, I tried this code without success

function GetClassElementNames (TObject ) : TStringlist ; var LCtx : TRttiContext; LMethod : TRttiMethod; begin try LCtx:=TRttiContext.Create; try // list the methods for the any class class for LMethod in LCtx.GetType(TObject).GetMethods do result.add(LMethod.Name); finally LCtx.Free; end; except on E: Exception do result.add (E.ClassName + ': ' + E.Message); end; end; 
+4
parameter-passing delphi
source share
1 answer

Use TClass for this, which TRttiContent.GetType() expects anyway.

You also do not assign a result before filling it out.

Try the following:

 function GetClassElementNames(Cls: TClass) : TStringlist ; var LCtx : TRttiContext; LMethod : TRttiMethod; begin Result := TStringList.Create; try LCtx := TRttiContext.Create; try for LMethod in LCtx.GetType(Cls).GetMethods do Result.Add(LMethod.Name); finally LCtx.Free; end; except on E: Exception do Result.Add(E.ClassName + ': ' + E.Message); end; end; 

 var Methods: TStringList; begin Methods := GetClassElementNames(TSomeClass); try ... finally Methods.Free; end; end; 

If you want to pass an instance of an object instead of a class type, you can wrap GetClassElementNames() as follows:

 function GetObjectElementNames(Object: TObject): TStringList; begin Result := GetClassElementNames(Object.ClassType); end; 

With that said, you should not return a new TStringList object. This is better and more flexible if the caller allocates a TStringList and passes it functions to populate, for example:

 procedure GetClassElementNames(Cls: TClass; AMethods: TStrings); var LCtx : TRttiContext; LMethod : TRttiMethod; begin try LCtx := TRttiContext.Create; try for LMethod in LCtx.GetType(Cls).GetMethods do AMethods.Add(LMethod.Name); finally LCtx.Free; end; except on E: Exception do AMethods.Add(E.ClassName + ': ' + E.Message); end; end; { procedure GetObjectElementNames(Object: TObject; AMethods: TStrings); begin GetClassElementNames(Object.ClassType, AMethods); end; } 

 var Methods: TStringList; begin Methods := TStringList.Create; try GetClassElementNames(TSomeClass, Methods); ... finally Methods.Free; end; end; 
+12
source share