42

In .Net, given a type name, is there a method that tells me in which assembly (instance of System.Reflection.Assembly) that type is defined?

I assume that my project already has a reference to that assembly, just need to know which one it is.

Fabio de Miranda
  • 1,096
  • 1
  • 8
  • 13

5 Answers5

42

Assembly.GetAssembly assumes you have an instance of the type, and Type.GetType assumes you have the fully qualified type name which includes assembly name.

If you only have the base type name, you need to do something more like this:

public static String GetAssemblyNameContainingType(String typeName) 
{
    foreach (Assembly currentassembly in AppDomain.CurrentDomain.GetAssemblies()) 
    {
        Type t = currentassembly.GetType(typeName, false, true);
        if (t != null) {return currentassembly.FullName;}
    }

    return "not found";
}

This also assumes your type is declared in the root. You would need to provide the namespace or enclosing types in the name, or iterate in the same manner.

jpj625
  • 1,138
  • 11
  • 6
  • I'd probably throw an ArgumentException in the case it can't find what you're looking for. Presumably that would be the exceptional case, and then you could also assume you found it (or put error handling code in a catch statement) – Matthew Scharley Jul 17 '09 at 02:20
  • BUT, Assembly.GetAssembly doesn't need an instance of the type, it only needs the type, so if you're looking for something that you know the type at compile-time, you can use typeof(Type) like in my first example. – Matthew Scharley Jul 17 '09 at 02:21
  • Thanks for the answers, it's working like a charm for me. I didn't have the Type, just the type name and know that a reference to an assembly containing it was available. – Fabio de Miranda Jul 17 '09 at 13:31
36
Assembly.GetAssembly(typeof(System.Int32))

Replace System.Int32 with whatever type you happen to need. Because it accepts a Type parameter, you can do just about anything this way, for instance:

string GetAssemblyLocationOfObject(object o) {
    return Assembly.GetAssembly(o.GetType()).Location;
}
Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
  • As a sidenote, I use that exact function as a way of populating the ReferencedAssemblies list when using the CSharpCodeProvider to compile C# dynamically. – Matthew Scharley Jul 17 '09 at 02:10
  • 1
    That's a useful tip, but the original poster only has the name of the type, not the Type instance. – John Bledsoe Jun 24 '11 at 12:13
3

I've adapted the accepted answer for my own purposes (returning the assembly object instead of the assembly name), and refactored the code for VB.NET and LINQ:

Public Function GetAssemblyForType(typeName As String) As Assembly
    Return AppDomain.CurrentDomain.GetAssemblies.FirstOrDefault(Function(a) a.GetType(typeName, False, True) IsNot Nothing)
End Function

I'm just sharing it here if anyone else would like a LINQy solution to the accepted answer.

MCattle
  • 2,897
  • 2
  • 38
  • 54
2
Type.GetType(typeNameString).Assembly
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 1
    That methods expects an assembly-qualified name. Are you sure it works cross-assembly if you don't qualify it with the assembly name? – chakrit Jul 17 '09 at 01:53
1

If you can use it, this syntax is the shortest/cleanest:

typeof(int).Assembly
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • 1
    Did you even read the OPs question before posting a generic answer like that? The OP does not have the type and only the string name. This answer completely ignores that fact. – Joshua Hayes Aug 18 '11 at 06:23
  • @JoshuaHayes You're making just as many assumptions as I did. When writing my generic answer, I started with just a type name (`int`) and wrote an expression to get the assembly where that type is defined. Not once in this entire thread did the OP say he had the type name as a string. – Sam Harwell May 02 '14 at 11:10
  • He did actually. Look at the method signature (string typeName). The OP said he only had the type name. Hence my reference to the string type name. – Joshua Hayes May 31 '15 at 23:08