如何使用Castle.Windsor在使用反射装入一个装配如何使用、反射、Castle、Windsor

2023-09-03 15:09:09 作者:落花浮王杯

让我们说我有一个图书馆Lib​​.dll,它使用Castle.Windsor初始化它的服务。

Let's say I have a library Lib.dll, which uses Castle.Windsor to initialize its services.

我有一个主要应用APP.EXE,使用反射它加载Lib.dll上运行。 APP.EXE不知道Lib.dll的位置预先,它仅已知在运行时。

I have a main application App.exe, which loads Lib.dll on runtime using reflection. App.exe does not know the location of Lib.dll beforehand, it is only known at runtime.

在此情况下,当APP.EXE负荷Lib.dll和Lib.dll初始化它的服务,System.TypeInitializationException异常被抛出,因为Castle.Windsor找不到服务类型。

In this case, when App.exe loads Lib.dll and Lib.dll initialize its services, a System.TypeInitializationException exception is thrown, because Castle.Windsor cannot find the service type.

Castle.MicroKernel.SubSystems.Conversion.ConverterException: Could not convert from 'Lib.TheServiceClass' to System.Type - Maybe type could not be found
   at Castle.MicroKernel.SubSystems.Conversion.TypeNameConverter.PerformConversion(String value, Type targetType) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\SubSystems\Conversion\TypeNameConverter.cs:line 91
   at Castle.MicroKernel.SubSystems.Conversion.DefaultConversionManager.PerformConversion(String value, Type targetType) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\SubSystems\Conversion\DefaultConversionManager.cs:line 134
   at Castle.MicroKernel.SubSystems.Conversion.DefaultConversionManager.PerformConversion[TTarget](String value) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\SubSystems\Conversion\DefaultConversionManager.cs:line 162
   at Castle.Windsor.Installer.DefaultComponentInstaller.SetUpComponents(IConfiguration[] configurations, IWindsorContainer container, IConversionManager converter) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Windsor\Installer\DefaultComponentInstaller.cs:line 196
   at Castle.Windsor.Installer.DefaultComponentInstaller.SetUp(IWindsorContainer container, IConfigurationStore store) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Windsor\Installer\DefaultComponentInstaller.cs:line 52
   at Castle.Windsor.WindsorContainer.Install(IWindsorInstaller[] installers, DefaultComponentInstaller scope) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Windsor\WindsorContainer.cs:line 327
   at Castle.Windsor.WindsorContainer.Install(IWindsorInstaller[] installers) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Windsor\WindsorContainer.cs:line 674

显然城堡找不到我的服务类,因为它是在Lib.dll未位于APP.EXE的目录。当我复制Lib.dll到APP.EXE目录中,问题消失,但不必复制这不是我们想要的东西。

Apparently Castle cannot find my service class because it is in Lib.dll that is not located in App.exe's directory. When I copy Lib.dll to App.exe directory, the problem goes away, but having to copy this is not something we want.

那么,如何让我的code在Lib.dll告诉Castle.Windsor加载类在正确的位置? (在Lib.dll的位置,而不是在APP.EXE位置)

So how can my code in Lib.dll tell Castle.Windsor to load the class in the correct location? (in Lib.dll location instead of in App.exe location)

推荐答案

您可以尝试通过加载在你的code悬而未决的组件的 AssemblyResolve 的事件

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.
};