gpt4 book ai didi

f# - Interop DLL 的实例化接口(interface)

转载 作者:行者123 更新时间:2023-12-01 04:43:09 24 4
gpt4 key购买 nike

我正在处理一些互操作代码,需要实例化一个接口(interface)。

C# 中的等价物是:

Interop.SomeInterface theInterface;
theInterface = new Interop.SomeInterface();
theInterface.SomeEvent += new SomeEventHandler(...);

如何在 F# 中完成对这个接口(interface)的实例化?我是否需要将此互操作 dll 重新包装成 F# 可以理解的东西?

最佳答案

无法实例化接口(interface)。 The C# compiler is pulling a trick通过查找接口(interface)的实现者来实例化一个类。这是由编译器检查 CoClassAttribute 来完成的。 .

据我所知,F# 对 CoClassAttribute 一无所知,因此您需要更改代码,使其实例化一个实际的类,而不是一个接口(interface)。

您可以通过使用 Reflector、ILdasm 或任何其他元数据检查器查看 SomeInterface 上的 CoClassAttribute 来找出您需要的类。如果互操作包装器是使用 tlbimp 或 Visual Studio 生成的,则通常该类将按约定命名为 SomeInterfaceClass。

关于f# - Interop DLL 的实例化接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13767924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com