gpt4 book ai didi

c# - 如何在使用反射加载的程序集中使用 CaSTLe.Windsor

转载 作者:太空宇宙 更新时间:2023-11-03 16:40:59 27 4
gpt4 key购买 nike

假设我有一个库 Lib.dll,它使用 CaSTLe.Windsor 来初始化它的服务。

我有一个主应用程序 App.exe,它使用反射在运行时加载 Lib.dll。 App.exe 事先不知道 Lib.dll 的位置,只有在运行时才知道。

在这种情况下,当 App.exe 加载 Lib.dll 和 Lib.dll 初始化其服务时,将抛出 System.TypeInitializationException 异常,因为 CaSTLe.Windsor 找不到服务类型。

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

显然 CaSTLe 找不到我的服务类,因为它位于不在 App.exe 目录中的 Lib.dll 中。当我将 Lib.dll 复制到 App.exe 目录时,问题消失了,但必须复制它不是我们想要的。

那么我在 Lib.dll 中的代码如何告诉 CaSTLe.Windsor 将类加载到正确的位置? (在 Lib.dll 位置而不是在 App.exe 位置)

最佳答案

您可以尝试通过AssemblyResolve 事件在您的代码中加载未解析的程序集

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

关于c# - 如何在使用反射加载的程序集中使用 CaSTLe.Windsor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7510693/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com