unit Container;
interface
uses
System.Generics.Collections;
type
GlobalContainer =
class
private
class var _ClassDict : TDictionary<TGUID, TClass>;
protected
class constructor Create;
class destructor Destroy;
public
class procedure RegisterClass<TInterface : IInterface; TRegClass :
class>;
class function GetClass<T : IInterface> : T;
end;
implementation
uses
System.SysUtils,
System.TypInfo;
{ GlobalContainer }
class constructor GlobalContainer.Create;
begin
_ClassDict := TDictionary<TGUID, TClass>.Create;
end;
class destructor GlobalContainer.Destroy;
begin
_ClassDict.Free;
end;
class function GlobalContainer.GetClass<T> : T;
var
LGUID : TGUID;
LClass : TClass;
LObj : TObject;
begin
LGUID := GetTypeData( TypeInfo( T ) ).Guid;
if _ClassDict.ContainsKey( LGUID )
then
begin
LClass := _ClassDict.Items[LGUID];
LObj := LClass.Create;
Supports( LObj, LGUID, Result );
end;
end;
class procedure GlobalContainer.RegisterClass<TInterface, TRegClass>;
var
LGUID : TGUID;
begin
LGUID := GetTypeData( TypeInfo( TInterface ) ).Guid;
_ClassDict.AddOrSetValue( LGUID, TRegClass );
end;
end.