4
votes

Idea is simple - making TDictionary with class name to TComponent to use

for enum in vm.ClassNameToComponent do
 TLuaClassTemplate<enum.Value>.RegisterClass(vm.LS, PrintGlobal, container, vm);

with enum instead of

TLuaClassTemplate<TButton>.RegisterClass(vm.LS, PrintGlobal, container, vm);
TLuaClassTemplate<TPanel>.RegisterClass(vm.LS, PrintGlobal, container, vm);
TLuaClassTemplate<TEdit>.RegisterClass(vm.LS, PrintGlobal, container, vm);
...

and to use class name taken from xml to work with generic based classes.
But there is problem:

TClassNameToComponentDict = TDictionary<string, TComponent>;
...
ClassNameToComponent: TClassNameToComponentDict;
...
  ClassNameToComponent := TClassNameToComponentDict.Create;
  ClassNameToComponent.Add('TButton', TButton);
  ClassNameToComponent.Add('TPanel', TPanel);
  ClassNameToComponent.Add('TEdit', TEdit);
...

error "Incompatible types 'TComponent' and 'class of TButton'".
How to use "class of" like TButton etc as generic value?

1

1 Answers

5
votes

The type you have used

TDictionary<string, TComponent>

represents a mapping from a string to an instance of a class. But you want a mapping from a string to the class. So you need:

TDictionary<string, TComponentClass>

where

TComponentClass = class of TComponent

Note that you do not need to declare TComponentClass since it is already declared in the Classes unit.