How to use Castle.Windsor in an assembly loaded using reflection

让人想犯罪 __ 提交于 2019-12-01 00:56:40

You can try to load the unresolved assemblies within your code by AssemblyResolve event

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
     string typeToLoad = args.Name;
     string myPath = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName;
     return Assembly.LoadFile(...); //or return Assembly.GetExecutingAssembly() etc.
};

You probably will consider loading the plugin in a separate AppDomain, with a different private path, look at AppDomainSetup. Of course there is a drawback that you neeed a separate app domain for your plugin, but sometimes this is considered a good practice.

You probably can't do it in easy and elegant way if App.exe doesn't provide you Castle Windsor's container instance to configure your services.

If it is not exposed directly, maybe you can access it using Service Locator? Or find it on your own using reflection on App.exe assembly?

The best solution will be if code in App.exe calls specific method in your library (i.e. it can look for particular interface implementation, like IModuleInitializer or something, create an instance of it and call some kind of Initialize method passing container instance to your code).

You could also think about extensibility frameworks like MEF, but that can be a bit overkill and make big influence on App.exe.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!