问题描述
我在CodeProject上发现这篇文章:
I found this article on CodeProject:http://www.codeproject.com/Articles/512956/NET-Shell-Extensions-Shell-Context-Menus
,并认为这将是很好的给它尝试,但在F#。所以我想出了以下代码:
and thought it would be nice to give it a try, but in F#. So I came up with the following code:
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),然后在项目属性 - >生成 - >其他标志 --keyfile:keyName.snk)。
However, I noticed that there is no support for signing an F# assembly in VS2012 (step 4. in the article). I learnt that if I want to do so, I need to create a key manually (typing "sn -k keyName.snk" into the command prompt) and then add a flag in "Project Properties -> Build -> Other Flags" (--keyfile:keyName.snk).
我仍然无法成功运行此操作。此外,使用作者的应用程序(在调试Shell扩展部分)我得到一个错误,我的程序集不包含COM服务器。
I still didn't manage to successfully run this. Moreover, using the author's application (in "Debugging the Shell Extension" section) I get an error that my assembly doesn't contain a COM server.
做错事与签署组件。
推荐答案
一个签署F#程序集的方法是通过 AssemblyFileKeyAttribute
One way to sign an F# assembly is via the AssemblyFileKeyAttribute
attribute.
创建一个新模块:
module AssemblyProperties
open System
open System.Reflection;
open System.Runtime.InteropServices;
[<assembly:AssemblyKeyFileAttribute("MyKey.snk")>]
do()
其中MyKey.snk
是相对于项目目录的密钥路径。
Where "MyKey.snk"
is the path to your key relative to the project directory.
另一种方式,在这个错误报告中找到,是将 - 密钥文件:MyKey.snk
添加到
标签。属性中的其他标志
字段>创建
Another way, as found in this bug report on Microsoft Connect, is to add --keyfile:MyKey.snk
to the Other Flags
field in the Properties --> Build
tab.
使用任一方法;运行 sn -v myfsharpassembly.dll
将断言程序集在编译后有效。
Using either approach; running sn -v myfsharpassembly.dll
will assert that the assembly is valid after compilation.
这篇关于签名F#程序集(强名称组件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!