Loading assemblies and versioning

前端 未结 3 584
有刺的猬
有刺的猬 2021-01-06 06:03

I\'m contemplating adding some extensibility into an existing app by providing a few predefined interfaces that can be implemented by \"plugins\" dropped at a specific locat

3条回答
  •  生来不讨喜
    2021-01-06 06:44

    Whatever your reasons may be to avoid your 3 points (which I think are more reliable and apt), you can take advantage of Assembly Resolve Event.

    Include code such as following in your core.dll

    static Assembly AppDomain_AssemblyResolve(object sender, ResolveEventArgs args)
           {
                //replace the if check with more reliable check such as matching the public key as well
                if (args.Name.Contains(Assembly.GetExecutingAssembly().GetName().Name))
                {
                    Console.WriteLine("Resolved " + args.Name + " as " + Assembly.GetExecutingAssembly().GetName());
                    return Assembly.GetExecutingAssembly();
                }
    
                return null;
           }
    

    Bind the above handler before attempting to load your plugins. If you load the plugins in current Appdomain then bind it to AssemblyResolve of current domain.

    For e.g.

    [SecurityPermission(SecurityAction.Demand, ControlAppDomain = true)]
    public static void LoadPlugins()
    {
        AppDomain.CurrentDomain.AssemblyResolve += AppDomain_AssemblyResolve;
        Assembly pluginAssembly = AppDomain.CurrentDomain.Load("MyPlugin");
    }
    

提交回复
热议问题