签署F#程序集(强名称组件)

时间:2013-07-21 21:16:25

标签: com f# strongname shell-extensions code-translation

我在CodeProject上发现了这篇文章: http://www.codeproject.com/Articles/512956/NET-Shell-Extensions-Shell-Context-Menus

并认为尝试一下会很好,但在F#中。所以我想出了以下代码:

open System
open System.IO
open System.Text
open System.Runtime.InteropServices
open System.Windows.Forms
open SharpShell
open SharpShell.Attributes
open SharpShell.SharpContextMenu

[<ComVisible(true)>]
[<COMServerAssociation(AssociationType.ClassOfExtension, ".txt")>]
type CountLinesExtension() =
    inherit SharpContextMenu.SharpContextMenu()

    let countLines =
        let builder = new StringBuilder()
        do 
            base.SelectedItemPaths |> Seq.iter (fun x -> builder.AppendLine(sprintf "%s - %d Lines" (Path.GetFileName(x)) (File.ReadAllLines(x).Length)) |> ignore  )
            MessageBox.Show(builder.ToString()) |> ignore

    let createMenu =
        let menu = new ContextMenuStrip()
        let itemCountLines = new ToolStripMenuItem(Text = "Count Lines")
        do
            itemCountLines.Click.Add (fun _ -> countLines)
            menu.Items.Add(itemCountLines) |> ignore
        menu

    override this.CanShowMenu() = true
    override this.CreateMenu() = createMenu

但是,我注意到在VS2012中没有支持签署F#程序集(文章中的第4步)。我了解到如果我想这样做,我需要手动创建一个密钥(在命令提示符下键入“sn -k keyName.snk”)然后在“项目属性 - &gt;构建 - &gt;其他标志中添加一个标志“( - keyfile:keyName.snk)。

我仍然无法成功运行此功能。此外,使用作者的应用程序(在“调试Shell扩展”部分中)我得到一个错误,我的程序集不包含COM服务器。

我相信我在签署组件时遇到了问题。你可以帮我管理吗?

2 个答案:

答案 0 :(得分:14)

签署F#程序集的一种方法是通过AssemblyFileKeyAttribute属性。

创建一个新模块并在其中放置:

module AssemblyProperties

open System
open System.Reflection;
open System.Runtime.InteropServices;

[<assembly:AssemblyKeyFileAttribute("MyKey.snk")>]

do()

其中"MyKey.snk"是您的密钥相对于项目目录的路径。

另一种方法,如Microsoft Connect上的错误报告中所示,是将--keyfile:MyKey.snk添加到Other Flags标签中的Properties --> Build字段。

使用任何一种方法;运行sn -v myfsharpassembly.dll将断言汇编在编译后有效。

答案 1 :(得分:0)

也许问题是装配不是“ComVisible”?

我在F#中使用这个库,如下所示:

[<assembly:ComVisible(true)>]
[<assembly:AssemblyKeyFile("Key.snk")>]
do
   ()

也就是说,我在顶级do-block指定汇编级属性。

相关问题