c# - 可疑的演员阵容中没有继承自的解决方案中的类型->为什么/以某种方式起作用
问题描述
我目前正在为 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 类型转换,互操作处理程序会自动调用IUnknown.QueryInterface
COM 对象(每个 COM 对象都实现 IUnknown),以判断它是否支持PartDocument
接口,并获取相关 VTable 的位置。这与 .NET 类型规范完全脱节,理论上,每次调用时都可能返回不同的结果。
推荐阅读
- r - R:如何从一起覆盖一个区域的多个图块创建单个图层
- sql - 使用多个 Case When 表达式加快查询运行时间
- java - 使用 Gson 在 Java 中解析 JSON
- gitlab-ci - 如何在 gitlab ci 中使用受保护的变量
- jquery - 加载内容后如何停止无限滚动
- windows - 如何在 Windows 上将可执行文件添加到 AWS Lambda .jar?
- postgresql - 如何在羽毛/反对 ORM 中查询 jsonb (postgtresql)?
- python - 如何基于 2 列从另一个数据框中获取数据
- linux - 从 Virtualbox 运行 awk 脚本时遇到困难
- encryption - 这是什么编码/加密方法?一系列 4 位数字