TypeCasting:下面两行代码有什么区别?
TypeCasting : what is difference between below 2 lines of code?
下面两行代码有什么区别。两者都试图获得路径,一个正在工作,另一个正在抛出错误。我正在研究 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
与 IDesigner
.
不同
(FFormOwner as TForm).Designer) as IDesigner
这对 IDesigner
执行运行时查询,并通过调用 QueryInterface
解决。这是从现有接口获取不同接口的正确方法。
下面两行代码有什么区别。两者都试图获得路径,一个正在工作,另一个正在抛出错误。我正在研究 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
与 IDesigner
.
(FFormOwner as TForm).Designer) as IDesigner
这对 IDesigner
执行运行时查询,并通过调用 QueryInterface
解决。这是从现有接口获取不同接口的正确方法。