Daniel Fisher (lennybacon.com)

SOA, DATA & THE WEB

ReadAssembylName a handy tool to get fully qualified assembly names

I'm currently working on a project together with Jürgen Bayer (autor of the C# 2008 Codebook and the Visual C# 2008 Kompendium) that makes use of a lot of dependency injection. The configuration files are blown with fully qualified assembly names but how do you get them? Working for instance with custom providers for ASP.NET membership, ASP.NET profile or ASP.NET roles gets you in the same circumstances. In the past I fired up Red Gate's Reflector and loaded the needed assembly to copy the name. I like Reflector but that approach becomes pretty annoying. So I fired up my IDE and hacked down the following peace of code:

using System;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.Win32;
 
namespace devcoach.Tools.AssemblyNameReader
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            // Install shell extension ...
            if (args == null || args.Length == 0)
            {
                using (var regkey =
                    Registry.CurrentUser.OpenSubKey("Software\\Classes", true))
                {
                    if (regkey == null) return;
                    using (var dllFileKey = regkey.CreateSubKey("dllfile"))
                    {
                        if (dllFileKey == null) return;
                        using (var shellKey = dllFileKey.CreateSubKey("shell"))
                        {
                            if (shellKey == null) return;
                            using (var readAsmNameKey =
                                shellKey.CreateSubKey("ReadAssemblyName"))
                            {
                                if (readAsmNameKey == null) return;
                                readAsmNameKey.SetValue(
                                        string.Empty,
                                        "Copy full name to clipboard...");
 
                                using (var commandKey =
                                   readAsmNameKey.CreateSubKey("command"))
                                {
                                    if (commandKey == null) return;
                                    commandKey.SetValue(
                                        "",
                                        string.Concat(
                                            "\"",
                                            Application.ExecutablePath,
                                            "\" \"%1\""));
                                }
                            }
                        }
                    }
                }
                return;
            }
 
            Assembly assembly = null;
            try
            {
                assembly = Assembly.LoadFile(args[0]);
            }
            catch(Exception){}
 
            if (assembly == null)
            {
                MessageBox.Show(
                    "Error loading assembly...",
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                return;
            }
            var name = assembly.FullName;
            if (string.IsNullOrEmpty(name)) return;
 
            Clipboard.SetDataObject(name, true);
        }
    }
}

Now I can use the a right click on any assembly to copy its fully qualified name to my clipboard.

image

Comments

Write a comment