Showing posts from July, 2020

How to load a DLL and runtime, invoke a method by name, get result object and use it as a parameter on another unkown DLL .NET Core

That post title took me a while to come up with, but my idea is pretty simple, what id you need sort of a bootstrap that don't reference DLLs, still be able to manage a simple operation of of invoking methods and pass results to the next DLL on the list. C# and .NET Core as a strong type based doesn't really have obvious ways to accomplish this, but i think i got to it with the following code, what do you think? string sourceAsmName = "DllToLoadName"; Assembly sourceAsm = AssemblyLoadContext.Default.LoadFromAssemblyPath(AppContext.BaseDirectory + sourceAsmName + @".dll"); AssemblyLoadHelper.LoadReferencedAssemblies(sourceAsm); Type sourceType = sourceAsm.GetType(sourceAsmName + "." + source); MethodInfo? sourceMethod = sourceType.GetMethod("extract"); object? sourceInstance = Activator.CreateInstance(sourceType, new object[] { _config, _logger }); Task sourceTask = (Task)sourceMethod.Invoke(sourceInstance, null); _logger.LogInformati