共用方式為


How to: Create a Tool to Get the Full Name of an Assembly

Applies to: SharePoint Foundation 2010

SharePoint Foundation development projects often involve a mixture of code and XML markup. Frequently, you will be adding your new assembly’s full four-part name to a project file, such as an XML configuration file. If your Visual Studio project is based on any of the SharePoint 2010 project templates in Visual Studio, you can simply insert the token $SharePoint.Project.AssemblyFullName$ where the full name of the assembly should be in most kinds of project files (but not .cs or .vb files or other compilable files). When you build and deploy the project, Visual Studio will replace the token with the full assembly name in the copies of the files that are actually deployed. (For more information about the use of SharePoint-oriented Visual Studio tokens, see Replaceable Parameters.)

In some cases, however, you may need to insert the full assembly name into a file type that does not support the Visual Studio tokens. There are also situations in which you are not using one of SharePoint 2010 project templates. For example, some extensions of SharePoint's administrative functionality involve only an ordinary Microsoft .NET Framework console application. In these situations, you will need to know the actual full assembly name. This topic explains how to create a command-line tool that gets the full name of any assembly, and how to add the tool as an item on Visual Studio’s Tools menu so that you can use it to obtain the full name of an assembly that you are developing.

To create a command-line tool that gets an assembly full name

  1. Open Visual Studio as an administrator by right-clicking the program in the Start menu and selecting Run as administrator.

  2. Create a new console application project. In the New Project dialog, select Visual C# or Visual Basic, then select Windows and choose the Console Application template. Name the application "GetAssemblyName". Then click OK.

  3. Replace the entire contents of the Program.cs (or Program.vb) file with one of the following examples (depending on your programming language).

    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
    
  4. Compile the application and test it.

  5. When you are satisfied that the tool functions correctly, copy the executable to a permanent folder on your computer.

To add a Get Assembly Full Name item to the Tools menu

  1. In Visual Studio, select External Tools from the Tools menu.

  2. In the External Tools dialog, click Add and enter Get Assembly Full Name for the Title.

  3. Fill the Command textbox by browsing to GetAssemblyName.exe.

  4. In the Arguments textbox, type the following (which is case sensitive): $(TargetPath)

  5. Enable the Use Output window checkbox.

  6. Click OK. The new command is added to the Tools menu.

Whenever you need the four-part name of the assembly you are developing, click the Get Assembly Full Name command on the Tools menu and the name will appear in the Output window.

Note

Because the full assembly name includes the Public Key Token which does not exist until the assembly is compiled the first time, you will need to compile the project before using the tool.

See Also

Concepts

How to: Create a Tool to Get the Public Key of an Assembly