private static void ExecuteAssembly(int i) { var context = new CollectibleAssemblyLoadContext(); var assemblyPath = Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "..", "..", "SampleLibrary", "bin", "Debug", "netstandard2.0", "SampleLibrary.dll"); using (var fs = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read)) { var assembly = context.LoadFromStream(fs); var type = assembly.GetType("SampleLibrary.Greeter"); var greetMethod = type.GetMethod("Hello"); var instance = Activator.CreateInstance(type); greetMethod.Invoke(instance, new object[] { i }); } context.Unload(); }
private static void ExecuteInMemoryAssembly(Compilation compilation, int i) { var context = new CollectibleAssemblyLoadContext(); using (var ms = new MemoryStream()) { var cr = compilation.Emit(ms); ms.Seek(0, SeekOrigin.Begin); var assembly = context.LoadFromStream(ms); var type = assembly.GetType("Greeter"); var greetMethod = type.GetMethod("Hello"); var instance = Activator.CreateInstance(type); var result = greetMethod.Invoke(instance, new object[] { i }); } context.Unload(); }