我在Delphi XE,Windows 7中工作.
在应用程序中,我想为我的用户启用不同的报告类型以供选择.为此,我有一个基本报告类和每个报告类型的子类(xml,csv,ppt等).
{Just an illustrating example}
TBaseReport = class
public
constructor Create;
procedure GenerateReport; virtual; abstract;
class function ReportType: string; virtual; abstract;
end;
T*Report = class(TBaseReport);
//Etcetera.
Run Code Online (Sandbox Code Playgroud)
我想要做的是使用Rtti检测所有报告类并列出他们的ReportType.之后,我想使用Rtti创建所选报告类的实例并调用GenerateReport.总而言之,这并不难实现.
但是有一个主要的缺点:我从不硬编码使用降序类,因此代码不会包含在可执行文件中.
有没有一种不错的方法来强制链接器/编译器包含这些类?
一个(丑陋的)解决方法是在初始化部分模拟报告的使用,但我宁愿不这样做.更好的解决方案是使基类持久化并调用'RegisterClass(T*Report);' 在初始化部分.它有效,但我没有看到任何其他需要使它们持久化,所以再次,我宁愿不这样做.另一方面,也许这是唯一的方法吗?
提前致谢.
我正在使用Delphi XE编写基类,这将允许降序类通过应用注释来映射dll方法.但是我得到了一个类型转换错误,这是可以理解的.
本质上,基类应如下所示:
TWrapperBase = class
public
FLibHandle: THandle;
procedure MapMethods;
end;
procedure TWrapperBase.MapMethods;
var
MyField: TRttiField;
MyAttribute: TCustomAttribute;
pMethod: pointer;
begin
FLibHandle := LoadLibrary(PWideChar(aMCLMCR_dll));
for MyField in TRttiContext.Create.GetType(ClassType).GetFields do
for MyAttribute in MyField.GetAttributes do
if MyAttribute.InheritsFrom(TMyMapperAttribute) then
begin
pMethod := GetProcAddress(FLibHandle, (MyAttribute as TMyMapperAttribute).TargetMethod);
if Assigned(pMethod) then
MyField.SetValue(Self, pMethod); // I get a Typecast error here
end;
Run Code Online (Sandbox Code Playgroud)
降序类看起来像这样:
TDecendant = class(TWrapperBase)
private type
TSomeDLLMethod = procedure(aParam: TSomeType); cdecl;
private
[TMyMapperAttribute('MyDllMethodName')]
FSomeDLLMethod: TSomeDLLMethod;
public
property SomeDLLMethod: TSomeDLLMethod read FSomeDLLMethod;
end; …Run Code Online (Sandbox Code Playgroud)