Wonkydonky
Wonkydonky
CC#
Created by Wonkydonky on 7/27/2024 in #help
✅ Loading types from an assembly fails because assemblies provided via project SDK cannot be loaded
I'm trying to load types from an assembly to analyze and execute them. The project of which I want to load the assembly is an ASP.NET Core project and uses the project SDK Microsoft.NET.Sdk.Web. Loading the built assembly with Assembly.LoadFile/Assembly.LoadFrom works fine. But as soon as I try to get types from the assembly (e.g. with GetTypes()) a System.Reflection.ReflectionTypeLoadException is thrown because one of the assemblies (e.g. Microsoft.Extensions.Hosting.Abstractions) cannot be located. It throws for multiple referenced assemblies, but all of these are included with the project SDK, yet don't appear in the output directory of the assembly unless I specifically publish as self-contained application, which is of no use here. How do I load the types of my target assembly while successfully resolving the referenced SDK assemblies? I could use the Types property of the ReflectionTypeLoadException, but I'd rather not get the exception at all.
25 replies