我在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“),然后在"Project Properties -> Build -> Other Flags”(--keyfile:keyName.snk)中添加一个标志。
我还是没能成功地运行它。此外,使用作者的应用程序(在“调试Shell扩展”部分),我得到一个错误,即我的程序集不包含COM服务器。
我相信我在对组件进行签名时做错了什么。你能帮我管理这个吗?
发布于 2013-08-11 01:35:39
对F#程序集进行签名的一种方法是通过AssemblyFileKeyAttribute属性。
创建一个新模块,并在其中放入:
module AssemblyProperties
open System
open System.Reflection;
open System.Runtime.InteropServices;
[<assembly:AssemblyKeyFileAttribute("MyKey.snk")>]
do()其中"MyKey.snk"是您的密钥相对于项目目录的路径。
另一种方法是将--keyfile:MyKey.snk添加到Properties --> Build选项卡中的Other Flags字段,如Microsoft Connect上的这篇bug报告所示。
使用任何一种方法;运行sn -v myfsharpassembly.dll将断言程序集在编译后是有效的。
发布于 2013-10-04 02:46:23
也许问题是程序集不是"ComVisible"?
我在F#中使用这个库的方式如下:
[<assembly:ComVisible(true)>]
[<assembly:AssemblyKeyFile("Key.snk")>]
do
()也就是说,我在顶级do-block中指定了程序集级属性。
https://stackoverflow.com/questions/17777014
复制相似问题