I have a class library in which I load dynamic assemblies. How can I load these assemblies using the new MetaDataLoadContext and MetaDataAssemblyResolver?
Code:
// Retrieve the domain assembly used by the compilation
Assembly baseAssembly = typeof(MyType).Assembly;
// Retrieve the location of the assembly and the referenced assemblies used by the domain
AssemblyName[] baseAssemblyReferences = baseAssembly.GetReferencedAssemblies();
var paths = new List<string>();
foreach (var item in baseAssemblyReferences)
{
paths.Add(item.Name + ".dll");
}
paths.Remove("mscorlib.dll");
paths.Remove("System.Runtime.dll");
paths.Remove("netstandard.dll");
paths.Remove("System.Core.dll");
paths.Remove("System.dll");
// Create PathAssemblyResolver that can resolve assemblies using the created list.
var resolver = new PathAssemblyResolver(paths);
var mlc = new MetadataLoadContext(resolver, baseAssmebly.GetName().Name);
I am getting an error:
Could not find assembly 'MYAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. Either explicitly load this assembly using a method such as LoadFromAssemblyPath() or use a Metadata AssemblyResolver that returns a valid assembly