unit EXCreateByClassName;
// 20101125 Thomas Wassermann
interface
uses RTTI,TypInfo,Controls,Classes;
type
TBaseObject=Class
Constructor Create(a:TValue);
virtual;
End;
TBaseObjectClass=
Class of TBaseObject;
Function CreateClassByClassName(
const s:
String;a:Tvalue):TObject;
Function CreateControlClassByClassName(
const s:
String;Owner:TComponent):TControl;
implementation
Function CreateClassByClassName(
const s:
String;a:Tvalue):TObject;
var
aClass : TClass;
context : TRttiContext;
types : TArray<TRttiType>;
aType : TRttiType;
begin
Result :=
nil;
context := TRttiContext.Create;
types := context.GetTypes;
for aType
in types
do
begin
if aType.TypeKind = tkClass
then
begin
aClass := aType.AsInstance.MetaclassType;
if aClass.ClassName = s
then
begin
Result := TBaseObjectClass(aClass).Create(a);
end;
end;
end;
end;
Function CreateControlClassByClassName(
const s:
String;Owner:TComponent):TControl;
var
aClass : TClass;
context : TRttiContext;
types : TArray<TRttiType>;
aType : TRttiType;
StrClass:TControl;
begin
Result :=
nil;
context := TRttiContext.Create;
types := context.GetTypes;
for aType
in types
do begin
if aType.TypeKind = tkClass
then begin
aClass := aType.AsInstance.MetaclassType;
if aClass.ClassName = s
then begin
Result := TControlClass(aClass).Create(Owner);
end;
end;
end;
end;
{ TBaseObject }
constructor TBaseObject.Create(a: TValue);
begin
inherited Create;
end;
end.