可疑演员 解决方案中没有继承自的类型--> 为什么/以某种方式工作

问题描述

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

  • 组装文件
  • 部分文档

api 为我提供了一个返回所选文档的方法。

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

在运行时此演员表有效。编译器告诉我这是一个可疑的转换,因为“PartDocument”没有实现“application.ActiveDocument”返回的类型。

 [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 类型转换,interop 处理程序会自动调用 COM 对象上的 IUnknown.QueryInterface(每个 COM 对象都实现 IUnknown),以确定它是否支持 PartDocument 接口,并获取相关 VTable 的位置。这与 .NET 类型规范完全脱节,理论上,每次调用它都可能返回不同的结果。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...