在运行时实现接口有什么好处?

What's the benefit of implement interface at runtime?

我正在努力理解 TVirtualInterface class。

{$APPTYPE CONSOLE}

uses
  SysUtils, Rtti;

type
  ISpecificInterface = interface(IInvokable)
    ['{281D8B97-397E-430A-895A-9CA4E1F5FB5F}']
    procedure SpecificProcedure;
  end;

procedure AProcedure(Method: TRttiMethod; const Args: TArray<TValue>;
  out Result: TValue);
begin
  Writeln(Method.ToString);
end;

var
  ISpecificInterfaceInstance: ISpecificInterface;

begin
  ISpecificInterfaceInstance := TVirtualInterface.Create
    (TypeInfo(ISpecificInterface), AProcedure) as ISpecificInterface;

  ISpecificInterfaceInstance.SpecificProcedure;

end. // TVirtualInterface ref. counter is decremented

在运行时实现接口有什么好处?

space有什么用?

描述是here

Provides functionality for remote procedure call marshaling...

有很多使用它的方法,但它们都有一个共同点,因为您使用的是接口,调用方不关心实现的外观 - 如果它是一个实际的 class那是在编译时实现接口,或者如果您在运行时通过 TVirtualInterface 或其他方式动态实现它,只要它的行为符合接口约定。

当它首次在 XE2 中引入时,我写了一篇 article about it 及其可能的用法。正如评论中提到的,这实际上使我们能够以一种非常简单的方式实现模拟框架,只需声明通用模拟类型,这些类型在内部创建一个接口代理来处理模拟中指定的调用。