Delphi: How to allow setting a TClass-property of a TCollectionItem at design time

467 Views Asked by At

I'm developing a component that works on several classes. In order to allow adding the list of managed classes, I've written a TCollection's inherited class in which each item (inherited from TCollectionItem) defines a published "TargetClassName" property. The "TargetClassName" property's setter function, calls the following function in order to find the corrisponding TClass:

function FindAnyClass(const Name: string): TClass;
var
  ctx: TRttiContext;
  typ: TRttiType;
  list: TArray<TRttiType>;
begin
  Result := nil;
  ctx := TRttiContext.Create;
  list := ctx.GetTypes;
  for typ in list do
    begin
      if typ.IsInstance and (EndsText(Name, typ.Name)) then
        begin
          Result := typ.AsInstance.MetaClassType;
          break;
        end;
    end;
  ctx.Free;
end;

(Thanks to Dalija Prasnikar for writing the function Get class by its name in Delphi).

Now, I'm wondering if there's a better way to allow adding classes to a TCollectionItem at design time.. What do you think about it? Hope to read interesting solutions! Thanks to all.

1

There are 1 best solutions below

1
On

in creation on TCollection You need To introduce Collation Class it's Posible in two way 1 : hard coded in create time X := TMycollation.Create(TMyCollationClass) 2 : your solution X := TMycollation.Create(FindAnyClass('TMyCollationClass'));