下面两行代码有什么区别。两者都在尝试获取路径,一个正在工作,另一个正在抛出错误。我正在研究 Delphi-7
Path:= (((FFormOwner as TForm).Designer) as IDesigner).GetPrivateDirectory; --Working
Path:= IDesigner(TForm(FFormOwner).Designer).GetPrivateDirectory ; --Error
下面是使用代码行获取路径的代码。
constructor TsampleComponent.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FFormOwner:=TForm(Owner);
if not (Owner is TForm) then
repeat
FFormOwner:=TForm(FFormOwner.Owner);
until (FFormOwner is TForm) or (FFormOwner.Owner=nil);
if (csDesigning in ComponentState) then
Path:= (((FFormOwner as TForm).Designer) as IDesigner).GetPrivateDirectory
else
Path:=ExtractFilePath(Application.EXEName);
.
.
end;
最佳答案
IDesigner(TForm(FFormOwner).Designer)
这将执行
Designer
的简单重新解释转换。它会失败,因为 Designer
的类型与 IDesignerHook
不同。(FFormOwner as TForm).Designer) as IDesigner
这将对
IDesigner
执行运行时查询,并通过调用 IDesigner
来解决。这是从现有接口(interface)获取不同接口(interface)的正确方法。关于delphi - 类型转换 : what is difference between below 2 lines of code?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30637832/