如何将stringname设置为泛型<t>的类

时间:2017-05-02 11:56:22

标签: delphi generics

我想来自字符串名称。但我不知道这是否可能。

我的代码:

function TServerMethods1.GetCustomer(aId: string): TJsonValue;
begin
  Result:=  ToJson(TRepositoryDatabase<TCustomer>.CreateRepository(Factories.FireBirdConnection.TFireBirdConnectionFactory.CreateConnecion(cConnectionString)).GetItem(aId));
end;

这很好用。 但现在我想把TCustomer设置为param。

function TServerMethods1.GetItem(aClassName, aId: string): TJsonValue;
var
  oClass: TClass;
  LCtx: TRttiContext;
  LTp: TRttiType;
begin
  try
    LTp := LCtx.FindType(AClassName);
  except
    Exit;
  end;

  if (LTp <> nil) and (LTp is TRttiInstanceType) then
    oClass := TRttiInstanceType(LTp).MetaclassType ;


  If assigned(oClass)
  then begin
    Result:=  ToJson(TRepositoryDatabase<oClass>.CreateRepository(Factories.FireBirdConnection.TFireBirdConnectionFactory.CreateConnecion(cConnectionString)).GetItem(aId));
  end;
end;

当我用oClass更改TCustomer时,我无法编译。 我得到[dcc32错误] ServerMethodsUnit1.pas(115):E2003未声明的标识符:&#39; TRepositoryDatabase&#39;

 TRepositoryDatabase <T: Class> = class(TRepositoryBase <T>, IRepository<T>)
  private
    FManager: TObjectManager;
  public
    constructor Create(aConnection: IDBConnection);
    destructor Destroy; override;

    procedure SaveItem(aItem: T); override;
    function GetItem(Id: Variant): T; override;
    function GetItems: TList <T>; override;
    procedure DeleteItem(aItem: T); override;

    class function CreateRepository (aConnection: IDBConnection): IRepository <T>;

  end;

是否可以从stringname创建一个类并在generecs中设置它?

0 个答案:

没有答案