F#中的ComImport

Nei*_*ell 2 f#

我正在尝试将一些代码从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{}
Run Code Online (Sandbox Code Playgroud)

我将其翻译为F#:

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

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

有没有解决方法?我正在尝试将应用程序移植到仅使用F#,所以如果不能用F#写这个项目将不得不重新考虑.

des*_*sco 5

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

let shellLink = 
    let ty = System.Type.GetTypeFromCLSID (System.Guid "00021401-0000-0000-C000-000000000046")
    Activator.CreateInstance ty
Run Code Online (Sandbox Code Playgroud)