views:

431

answers:

3

I have an assembly qualified name of a type, e.g.

MyNamespace.MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

I want to extract the assembly full name, i.e.

MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Obviously I could do this with simple string parsing, but is there a framework method for doing this?

Note: I don't have the type or assembly, just the string, and this is an essential part of the problem, so myType.AssemblyQualifiedName, myType.Assembly.FullName, etc isn't going to help

A: 

Here:

public string AssemblyName(string assemblyQualifiedName) {
    Type type = Type.GetType(assemblyQualifiedName, false);
    if(type == null) {
         return Parse(assemblyQualifiedName);
    }
    return type.Assembly.Name;
}

Edit: Wait. You don't have the assembly? Sorry to be the bearer of bad news, but then you need to parse.

Jason
Unfortunately my application won't have access to the actual type for me to call GetType at the time i have to perform the parsing...
Paul Killick
@Paul K: Yeah, if you don't have the assembly then you need to parse the string. Reflection etc. won't help you here as the CLR can't find the metadata for the type to perform reflection over. I try to avoid questioning motives, but what's wrong with parsing here?
Jason
@Jason, thx for your input... nothing wrong with parsing if that's what I have to resort to, but the FQN can get involved (MSDN "Specifying Fully Qualified Type Names"), and I just though a publicly exposed framework solution might exist since it must have to parse these things internally anyway...
Paul Killick
A: 

If you want to use GetType you'll need to load the assembly to the current domain. You can learn how to do it here.

Although it seems like a lot of work just to avoid trivial parsing...

Dror Helper
A: 

An overload to Type.GetType accepts an function that can be used to resolve the AssemblyName to an assembly. Returning null would normally throw an exception since the type cannot be resolved, but this can be suppressed by passing false to the throwOnError parameter.

The function used to resolve can also set a string variable in the outer scope that the original code will return.

using System;
using System.Diagnostics;
using System.Reflection;

namespace ConsoleApp {
    public static class Program {
        public static void Main() {
            var assemblyName = GetAssemblyName("MyNamespace.MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
            Debug.Assert(assemblyName == "MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
        }

        public static String GetAssemblyName(String typeName) {
            String assemblyName = null;
            Func<AssemblyName, Assembly> assemblyResolver = name => {
                assemblyName = name.FullName;
                return null;
            };

            var type = Type.GetType(typeName, assemblyResolver, null, false);
            if (type != null)
                return type.AssemblyQualifiedName;

            return assemblyName;
        }
    }
}
Simon Svensson