如何:创建用于获取程序集全名的工具
上次修改时间: 2010年7月6日
适用范围: SharePoint Foundation 2010
SharePoint Foundation 开发项目通常涉及混合的代码和 XML 标记。通常,可以在项目文件(例如 XML 配置文件)中添加新程序集完整的四部分名称。如果 Visual Studio 项目基于 Visual Studio 中的任意 SharePoint 2010 项目模板,则只需插入标记 $SharePoint.Project.AssemblyFullName$,其中,程序集的全名应为大多数种类的项目文件(但不是 .cs, .vb 文件或其他可编译的文件)。在构建和部署项目时,Visual Studio 会在实际部署的文件副本中将标记替换为程序集的全名。(有关使用面向 SharePoint 的 Visual Studio 标记的详细信息,请参阅可替换的参数。)
不过,在某些情况下,可能需要在不支持 Visual Studio 标记的文件类型中插入程序集的全名。也可能会不使用任何一个 SharePoint 2010 项目模板。例如,SharePoint 管理功能的某些扩展只涉及普通的 Microsoft .NET Framework 控制台应用程序。在这些情况下,需要知道程序集实际的全名。本主题介绍如何创建用于获取任意程序集的全名的命令行工具,以及如何将工具添加为 Visual Studio"工具"菜单上的项目,以便用其获取所开发程序集的全名。
创建用于获取程序集全名的命令行工具
右键单击"开始"菜单中的 Visual Studio,然后选择"以管理员身份运行",以管理员身份打开该程序。
创建一个新的控制台应用程序项目。在"新建项目"对话框中,选择"Visual C#"或"Visual Basic",然后选择"Windows"并选择"控制台应用程序"模板。将此应用程序命名为"GetAssemblyName"。然后单击"确定"。
用以下示例之一(根据编程语言)替换 Program.cs(或 Program.vb)文件的整个内容。
using System; using System.Reflection; using System.IO; namespace GetAssemblyName { class Program { static void PrintUsage() { Console.WriteLine("Usage: GetAssemblyName.exe <path and filename>\n"); Console.WriteLine(@"Example: GetAssemblyName.exe C:\MyAssembly.dll"); Console.Read(); } static void Main(string[] args) { if (args.Length < 1 || args[0] == "?") { PrintUsage(); return; } string filename = args[0]; try { AssemblyName an = AssemblyName.GetAssemblyName(filename); Console.WriteLine("Fully specified assembly name:\n"); Console.WriteLine(an.ToString()); } catch (FileNotFoundException) { Console.WriteLine("Cannot locate the assembly. Check the path and try again."); } Console.Read(); } } }
Imports System Imports System.IO Imports System.Reflection Module Module1 Sub PrintUsage() Console.WriteLine("Usage: GetAssemblyName.exe <path and filename>" + vbCrLf) Console.WriteLine("Example: GetAssemblyName.exe C:\MyAssembly.dll") Console.Read() End Sub Sub Main(ByVal cmdArgs() As String) If cmdArgs.Length = 0 OrElse cmdArgs(0) = "?" Then PrintUsage() Return End If Dim filename As String = cmdArgs(0) Try Dim an As AssemblyName = AssemblyName.GetAssemblyName(filename) Console.WriteLine("Fully specified assembly name:" + vbCrLf) Console.WriteLine(an.ToString()) Catch Console.WriteLine("Cannot locate the assembly. Check the path and try again.") End Try Console.Read() End Sub End Module
编译并测试该应用程序。
如果工具正常运行,请将可执行文件复制到计算机上的永久文件夹中。
向"工具"菜单中添加"获取程序集全名"项目
在 Visual Studio 中,从"工具"菜单中选择"外部工具"。
在"外部工具"对话框中,单击"添加"并为"标题"输入获取程序集全名。
通过浏览到 GetAssemblyName.exe 填充"命令"文本框。
在"参数"文本框中,键入下面的内容(区分大小写):$(TargetPath)
启用"使用输出窗口"复选框。
单击"确定"。新命令将添加到"工具"菜单中。
每当需要所开发程序集的四部分名称时,都可以在"工具"菜单上单击获取程序集全名命令,随后"输出"窗口中将显示该名称。
备注
由于程序集全名包括公钥标记,它在首次编译程序集之前不存在,因此需要在使用此工具之前编译项目。