How to get the assembly (System.Reflection.Assembly) for a given type in .Net?

62,766

Solution 1

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.

Solution 2

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;
}

Solution 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.

Solution 4

Type.GetType(typeNameString).Assembly
Share:
62,766
Fabio de Miranda
Author by

Fabio de Miranda

Computer Science Professor, programmer, PhD Student at Computer Vision-related topic.

Updated on September 06, 2020

Comments

  • Fabio de Miranda
    Fabio de Miranda over 3 years

    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.

  • chakrit
    chakrit almost 15 years
    That methods expects an assembly-qualified name. Are you sure it works cross-assembly if you don't qualify it with the assembly name?
  • Matthew Scharley
    Matthew Scharley almost 15 years
    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
    Matthew Scharley almost 15 years
    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
    Matthew Scharley almost 15 years
    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.
  • Fabio de Miranda
    Fabio de Miranda almost 15 years
    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.
  • John Bledsoe
    John Bledsoe almost 13 years
    That's a useful tip, but the original poster only has the name of the type, not the Type instance.
  • Joshua Hayes
    Joshua Hayes almost 13 years
    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.
  • Sam Harwell
    Sam Harwell about 10 years
    @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.
  • Joshua Hayes
    Joshua Hayes almost 9 years
    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.