views:

515

answers:

3

I would like to parse an assembly qualified name in .NET 3.5. In particular, the assembly itself is not available, it's just the name. I can think of many ways of doing it by hand but I guess I might be missing some feature to do that in the system libraries. Any suggestion?

A: 

Check out the Path class. It has a bunch of parsing functions for file names. Or you could try:

string qualType = @"System.String, mscorlib, Version=2.0.0.0, Culture=neutral, publicKeyToken=b77a5c561934e089";
Type t = Type.GetType(qualType, false); 
if (t == null) 
{
   Console.WriteLine("Invalid qualified type string.");
   return;
}
Console.WriteLine(t.FullName);

So you have something like this?

Assembly assem = Assembly.LoadFile("PathToTheAssembly.dll");
Type t = assem.GetType("Namespace.And.Type.Name");
t.FullName

If youre looking to parse the BNF form, the example above using GetType should work for you.

EDIT: Ok, this should do what you want assuming you have the name:

Assembly assem = Assembly.GetAssembly(Type.GetType(assembly_qualified_name, false));
if(assem != null)
{
   byte[] pkt = assem.GetName().GetPublicKeyToken();
   Version ver = assem.GetName().Version;
   CultureInfo ci = assem.GetName().CultureInfo();
}
SwDevMan81
The assembly itself is not available, so I can't go for a Type.GetType.
Joannes Vermorel
So just the string assembly name? Can you post what you actually have?
SwDevMan81
A: 

If assembly loaded you can use something like that:

    Assembly assembly = Assembly.GetExecutingAssembly();
    string assemblyName = assembly.GetName().Name;

In the example above I used an executing assembly but you loop through your loaded assembly.

Update: You always can load an assembly in a separate AppDomain get the assembly name and after you are done, unload it. Let me know if you need a sample.

Vadim
Well, precisely, I don't have the assembly at hand. I would like to parse the string representing its name.
Joannes Vermorel
@Joannes, see update part in the answer.
Vadim
+5  A: 

The AssemblyName class can parse the assembly name for you, just pass in the string to its constructor. If you have an assembly qualified type name, I think you'll have to strip of the type part of the string first (ie everything up to the first comma).

Mattias S