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

Alo*_*zcz 9 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
Run Code Online (Sandbox Code Playgroud)

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

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

我相信我在签署组件时遇到了问题.你可以帮我运行吗?

bri*_*ler 14

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

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

module AssemblyProperties

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

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

do()
Run Code Online (Sandbox Code Playgroud)

"MyKey.snk"密钥相对于项目目录的路径在哪里.

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

使用任何一种方法 运行sn -v myfsharpassembly.dll将断言编译后程序集有效.