113

Is there something like:

AppDomain.CurrentDomain.GetAssemblyByName("TheAssemblyName")

so instead of looping through AppDomain.CurrentDomain.GetAssemblies(), we could just get the specific assembly directly.

wonea
  • 4,783
  • 17
  • 86
  • 139
Jronny
  • 2,164
  • 4
  • 30
  • 41

7 Answers7

121

Have you tried looking at Assembly.Load(...)?

Community
  • 1
  • 1
cyberzed
  • 2,026
  • 1
  • 16
  • 26
  • 9
    For those who are wondering, this throws `System.IO.FileNotFoundException` if the assembly could not be loaded. – Rudey Jan 18 '18 at 16:57
  • Consider also `public static System.Reflection.Assembly LoadFrom (string assemblyFile);` – joe_inz Sep 16 '22 at 14:18
94

I resolved with LINQ

Assembly GetAssemblyByName(string name)
{
    return AppDomain.CurrentDomain.GetAssemblies().
           SingleOrDefault(assembly => assembly.GetName().Name == name);
}
Fabio
  • 1,890
  • 1
  • 15
  • 19
  • 10
    This will only work if the assembly in question is loaded. May not help when registering types in a DAL from a Web API – Chazt3n Jan 05 '15 at 21:12
19

It depends on what you're trying to accomplish.

If you just want to get the assembly, then you should call System.Reflection.Assembly.Load() (as already pointed out). That's because .NET automatically checks if the assembly has already been loaded into the current AppDomain and doesn't load it again if it has been.

If you're just trying to check whether the assembly has been loaded or not (for some diagnostics reason, perhaps) then you do have to loop over all the loaded assemblies.

Another reason you might want to loop is if you know only some of the assembly information (eg. you're not sure of the version). That is, you know enough to "recognise it when you see it", but not enough to load it. That is a fairly obscure and unlikely scenario, though.

EMP
  • 59,148
  • 53
  • 164
  • 220
17

If this is an assembly you have referenced, I like to write a class like the following:

namespace MyLibrary {
   public static class MyLibraryAssembly {
      public static readonly Assembly Value = typeof(MyLibraryAssembly).Assembly;
   }
}

and then whenever you need a reference to that assembly:

var assembly = MyLibraryAssembly.Value;
Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80
  • 1
    This is a very clean solution that I've been always looking for! I can use it like this: `options.RegisterFromAssembly(Application.AssemblyReference.Value)` – Alielson Piffer Aug 27 '20 at 02:46
  • This should be the accepted answer since it does not rely on some provided name. – Alexei - check Codidact Aug 04 '22 at 08:05
  • Obviously, if it is a third-party assembly that you need a reference to, you cannot add a helper class like that, but you can simply pick any type that you know to be part of the assembly, e.g. `SomeThirdPartyLibraryClass`, and get the assembly reference as `typeof(SomeThirdPartyLibraryClass).Assembly`. A very nice tip! – Otto G Aug 16 '23 at 22:37
9

For those who just need to access the assembly's metadata (version, etc.) check out Assembly.ReflectionOnlyLoad(name), which is able to load only the metadata, possibly saving on memory and IO.

  • 2
    @erik-reppen: According to MS docs it is available in previous versions. I'm using it in a 3.5 app: http://msdn.microsoft.com/en-us/library/0et80c7k(v=vs.100).aspx – codenheim Apr 21 '14 at 07:17
  • This would have been perfect for my use case, but alas it's not supported in .NET Core (as of 3.1) – OrdinaryOrange Oct 12 '21 at 22:51
5

You can write an extension method that does what you need.

This method will only enumerate loaded assemblies, if you possibly need to load it, use Assembly.Load from the accepted answer.

public static class AppDomainExtensions
{
    public static Assembly GetAssemblyByName(this AppDomain domain, string assemblyName)
    {
        return domain.GetAssemblies().FirstOrDefault(a => a.GetName().Name == assemblyName);
    }
}

Then you call this method on an AppDomain like this:

Assembly a = AppDomain.CurrentDomain.GetAssemblyByName("SomeAssembly")

If SomeAssembly is loaded into the current AppDomain the method will return it, otherwise it will return null.

Marcell Toth
  • 3,433
  • 1
  • 21
  • 36
2

Have a look at the System.Reflection.Assembly class, in particular the Load method: MSDN

Maximilian Mayerl
  • 11,253
  • 2
  • 33
  • 40