可疑转换在继承自的解决方案中没有类型--> why/somehow 它正在工作

suspicious cast there is no type in the solution which is inherited from--> why/somehow it is working

我目前正在为 Inventor(3d 建模软件)开发插件。当您使用 api 时,有两种不同类型的文档

api 为我提供了一种方法,可以 returns 选择文档。

   PartDocument part = ((PartDocument)application.ActiveDocument);

在运行时此转换有效。编译器告诉我这是一个可疑的转换,因为 'PartDocument' 没有实现 'application.ActiveDocument' returns.

类型
 [TypeLibType(TypeLibTypeFlags.FDispatchable)]
  [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
  [Guid("xxxxxx")]
  [DefaultMember("Type")]
  [ComImport]
  public interface PartDocument
  {
....
 [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
  [TypeLibType(TypeLibTypeFlags.FDispatchable)]
  [Guid("xxxxx")]
  [DefaultMember("Type")]
  [ComImport]
  public interface Application
  {...

    [DispId(50331905)]
    _Document ActiveDocument { [DispId(50331905), MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] [return: MarshalAs(UnmanagedType.Interface)] get; }
[Guid("xxxxxx")]
  [CoClass(typeof (_DocumentClass))]
  [ComImport]
  public interface _Document : Document, _VbaImplementationEvents_Event
  {
  }

那么这为什么有效?有人可以给我解释一下吗?

我该如何测试它?

var documentMock =new Mock<PartDocument>();
var applicationMock = new Mock<Application>();
applicationMock.Setup(x => x.ActiveDocument).Returns(documentMock.Object);

编译器告诉我他不能从 'PartDocument' 转换为 '_Document' --> 这是真的,那么为什么它在运行时工作?

提前致谢

之所以有效,是因为它是 COM 类型。对于 COM 类型转换,互操作处理程序将自动调用 COM 对象上的 IUnknown.QueryInterface(每个 COM 对象都实现了 IUnknown)以查明它是否支持 PartDocument 接口,并获取相关 VTable 的位置。这完全脱离了 .NET 类型规范,并且理论上,每次调用时可能 return 不同的结果。