1

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.

Community
  • 1
  • 1
Hwau
  • 850
  • 3
  • 12
  • 23
  • RTTI is almost surely the wrong solution. – David Heffernan Apr 11 '15 at 17:14
  • That's why I'm looking for a better solution. For the moment, I didn't found any other way to add classes at design time. – Hwau Apr 11 '15 at 17:22
  • 1
    I wouldn't suggest a solution without a better idea of what the problem is. As it stands you've presented your solution but not really described the problem. – David Heffernan Apr 11 '15 at 17:25
  • 1
    This is the type of situation where a small list of registered classes is probably a better choice. Have each desired class register itself at startup, and then you can loop through that list when needed. That will work at runtime, anyway. For design-time, you will likely have to write a custom property editor that enumerates the project's units looking for classes that have a common ancestor, or implement a common interface, or are marked with a common attribute, etc. – Remy Lebeau Apr 11 '15 at 17:34
  • @David Heffernan: The problem is that I don't really know any appreciable way to set a TClass property at design time. The one exposed, is just a "fallback solution" that uses the classname to get the TClass. I would like to find out a clean solution. Please, tell me which part is unclear and I'll try to explain it better. – Hwau Apr 11 '15 at 17:42
  • Which classes would you like to set at designtime? And with what purpose? Are those the classes of components? Other classes too? Can you give an example? – NGLN Apr 11 '15 at 18:15
  • Classes that could be added should be all inherited from TComponent. My component has a function named "LoadComponents(Target : TComponent)" that adds components that match with one of the managed classes (if Target.Components[i] is Self.FClasses[j] then ...). The goal is that to fill a variable number of arrays with components. In this way, as an example, I can easily apply changes to all components whose class is inherited by one of the managed classes (...Or any other thing I need to do with them). – Hwau Apr 11 '15 at 18:51
  • I still can't work out what you are trying to do. Never mind. – David Heffernan Apr 11 '15 at 20:07
  • That's just `TList` surely – David Heffernan Apr 11 '15 at 23:13
  • No. But I don't see why you need to write your entire program using the form designer. – David Heffernan Apr 12 '15 at 05:45
  • I haven't answered. I commented. I still don't see why you need to do this at design time. A lot of people fall into the trap of trying to do everything at design time rather than writing code. Perhaps that's happening here. – David Heffernan Apr 12 '15 at 11:40
  • I guess you know more about all this than I do. You don't need my help. Good luck. – David Heffernan Apr 13 '15 at 05:56

1 Answers1

0

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'));

dawood karimy
  • 179
  • 2
  • 13
  • My goal is that to set a TComponentClass proprerty at design time. Something like this: TMyCollectionItem = class(TCollectionItem) .... published MyProperty : TComponentClass ...; // <-- I would like to set this at design time end; – Hwau Apr 12 '15 at 13:16