2

我正在尝试将一些代码从 C# 转换为 F#,特别是创建快捷方式的代码,从这里开始:http: //vbaccelerator.com/home/NET/Code/Libraries/Shell_Projects/Creating_and_Modifying_Shortcuts/ShellLink_Code_zip_ShellLink/ShellLink_cs.asp

C# 中的代码如下:

[GuidAttribute("00021401-0000-0000-C000-000000000046")]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComImportAttribute()]
private class CShellLink{}

我将其翻译为 F#:

[<GuidAttribute("00021401-0000-0000-C000-000000000046")>]
[<ClassInterfaceAttribute(ClassInterfaceType.None)>]
[<ComImportAttribute()>]
type CShellLink() = class end

不幸的是,当我切换到 F# 实现时,我得到一个运行时错误:“导入中具有非零 RVA 的方法”。这似乎与此处报告的错误相同:http: //social.msdn.microsoft.com/Forums/en-US/fsharpgeneral/thread/dada2004-5218-4089-8918-eed2464bbbcd

有什么解决方法吗?我正在尝试将应用程序移植为仅使用 F#,因此如果不能用 F# 编写该项目,则必须重新考虑该项目。

4

1 回答 1

5

这看起来像是 F# 编译器中的一个限制:它不能使用 ComImportAttribute 定义现有的 COM 类,它只适用于接口。您可以将此作为解决方法吗?

let shellLink = 
    let ty = System.Type.GetTypeFromCLSID (System.Guid "00021401-0000-0000-C000-000000000046")
    Activator.CreateInstance ty
于 2012-04-30T22:10:25.490 回答