Assembly Loading in .NET Core

前端 未结 2 960
眼角桃花
眼角桃花 2021-01-03 03:59

Using VS2017 RC, .NET Core

I am trying to load an assembly from a file. The dependencies of this assembly are in the same folder.

I am using AssemblyL

2条回答
  •  轮回少年
    2021-01-03 04:35

    There is a great enhancement in .Net Core 3.0+, wire AssemblyLoadContext.Default.Resolving event as given below and all dependencies will be resolved and loaded:

     AssemblyLoadContext.Default.Resolving += (context, name) => {
                    string assemblyPath = $"{pluginFolder}\\{name.Name}.dll";                
                    if (assemblyPath != null)   
                        return context.LoadFromAssemblyPath(assemblyPath);     
                    return null;
                };
    

    Remember to define the variabe pluginFolder

    Solution2

    You can use the AssemblyDependencyResolver class and resolve dependendencies including ones in .deps.json:

      var resolver = new AssemblyDependencyResolver(pluginPath);            
    
      AssemblyLoadContext.Default.Resolving += (context, name) => {
    
                    string assemblyPath = resolver.ResolveAssemblyToPath(name);               
                    if (assemblyPath != null)   
                        return context.LoadFromAssemblyPath(assemblyPath);     
                    return null;
                };
    

提交回复
热议问题