Problems loading assembly dependencies dynamically at run-time

前端 未结 3 1305
逝去的感伤
逝去的感伤 2021-01-06 08:11

let me try to explain my problem. I\'m currently trying to develop a small \"plugin-framework\" written in .Net (mainly for experimenting a bit). So the idea is to have a ma

3条回答
  •  无人及你
    2021-01-06 08:45

    The AssemblyResolve event happens when the framework tries to load an assembly, and fails.

    What this means is that if it's giving you Presentation.Zune.dll in the args, that the framework can't find that assembly, and this is your chance to intercept it and do other things, like load it from a directory that the framework might not know about - eg your plugins\dependencies folder...

    Off the top of my head, I'd try something like this:

    Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        if( File.Exists(".\\Plugins\\"+args.Name) )  // it's a plugin
            return Assembly.Load(".\\Plugins\\"+args.Name);
        else if( File.Exists(".\\Plugins\\Dependencies\\"+args.Name) )  // it's a dependency OF a plugin
            return Assembly.Load(".\\Plugins\\Dependencies\\"+args.Name);
        else
            throw new Exception();
    }
    

提交回复
热议问题