jeudi 5 octobre 2017

C# - Getting all assemblies through reflection and their types

What I am trying to achieve is get all assemblies from solution with reflection and then their types, so I can have them inside collection Type[] using .NET Core 2.0.

What I can use is

  • AppDomain.CurrentDomain.GetAssemblies(); - the problem is that this will get all referenced and used assemblies. In my case I want to get all assemblies that are referenced, but not necessary used. So the problem is that JIT compilation is removing references for projects that are not used, so in that case this wouldn't work.
  • Other option is to get location of DLLs using Assembly.GetExecutingAssembly().Location and then load all assemblies. The problem here is that, when latter I want to use .GetTypes() this throw exception from type System.Reflection.ReflectionTypeLoadException and then loader exception I have System.IO.FileNotFoundException: Could not load file or assembly Microsoft.EntityFrameworkCore. The issue, is that it's trying to use libraries, which cannot find, apperantly loading DLLs using Assembly.LoadFrom(dll) doesn't seem to load dependencies (libraries) as well.

What I found is BuildManager.GetReferencedAssemblies, from what I read so far, it's saying that this could work and it's getting all references, even if they're not used, so I also found it in MsBuild library, but doesn't contains that method, so I might be referencing wrong package.

I am open for any suggestions, how I can load all solution assemblies and their types from .NET Core 2.0 project.

Thanks in advance!





Aucun commentaire:

Enregistrer un commentaire