使用 Unity 改变构造函数注入的字符串参数
我的目标是改变字符串参数:
Container
.RegisterInstance<string>("us", @"\\ad1\accounting$\Xml\qb_us.xml")
.RegisterInstance<string>("intl", @"\\ad1\accounting$\Xml\qb_intl.xml");
driver = Container.Resolve<LoaderDriver>(args[1]); // "us" or "intl"
这会导致:
Resolution of the dependency failed, type = "QuickBooksService.LoaderDriver", name = "intl".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type String cannot be constructed. You must configure the container to supply this value.
-----------------------------------------------
At the time of the exception, the container was:
Resolving QuickBooksService.LoaderDriver,intl
Resolving parameter "reader" of constructor QuickBooksService.LoaderDriver(QuickBooksService.LoaderInputReader reader, QuickBooksService.ILoader[] loaders)
Resolving QuickBooksService.LoaderInputReader,(none)
Resolving parameter "inputFile" of constructor QuickBooksService.LoaderInputReader(System.String inputFile, AccountingBackupWeb.Models.AccountingBackup.Company company, Qu
ickBooksService.eTargets targets)
Resolving System.String,(none)
这显然是错误的,但这是我可以让它工作的唯一方法:
if (args[1] == "us")
Container
.RegisterType<LoaderInputReader>(
new InjectionConstructor(
@"\\ad1\accounting$\Xml\qb_us.xml",
new ResolvedParameter<Company>(),
new ResolvedParameter<eTargets>()
)
)
;
else if (args[1] == "intl")
Container
.RegisterType<LoaderInputReader>(
new InjectionConstructor(
@"\\ad1\accounting$\Xml\qb_intl.xml",
new ResolvedParameter<Company>(),
new ResolvedParameter<eTargets>()
)
)
;
else
throw new Exception("invalid company");
driver = Container.Resolve<LoaderDriver>();
My goal is to vary a string parameter:
Container
.RegisterInstance<string>("us", @"\\ad1\accounting$\Xml\qb_us.xml")
.RegisterInstance<string>("intl", @"\\ad1\accounting$\Xml\qb_intl.xml");
driver = Container.Resolve<LoaderDriver>(args[1]); // "us" or "intl"
Which results in:
Resolution of the dependency failed, type = "QuickBooksService.LoaderDriver", name = "intl".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type String cannot be constructed. You must configure the container to supply this value.
-----------------------------------------------
At the time of the exception, the container was:
Resolving QuickBooksService.LoaderDriver,intl
Resolving parameter "reader" of constructor QuickBooksService.LoaderDriver(QuickBooksService.LoaderInputReader reader, QuickBooksService.ILoader[] loaders)
Resolving QuickBooksService.LoaderInputReader,(none)
Resolving parameter "inputFile" of constructor QuickBooksService.LoaderInputReader(System.String inputFile, AccountingBackupWeb.Models.AccountingBackup.Company company, Qu
ickBooksService.eTargets targets)
Resolving System.String,(none)
This is obviously wrong but its the only way I could get it to work:
if (args[1] == "us")
Container
.RegisterType<LoaderInputReader>(
new InjectionConstructor(
@"\\ad1\accounting$\Xml\qb_us.xml",
new ResolvedParameter<Company>(),
new ResolvedParameter<eTargets>()
)
)
;
else if (args[1] == "intl")
Container
.RegisterType<LoaderInputReader>(
new InjectionConstructor(
@"\\ad1\accounting$\Xml\qb_intl.xml",
new ResolvedParameter<Company>(),
new ResolvedParameter<eTargets>()
)
)
;
else
throw new Exception("invalid company");
driver = Container.Resolve<LoaderDriver>();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样的东西应该可以工作:
这为
LoaderInputReader
的每个注册命名。现在你可以这样解决:Something like this ought to work:
This names each registration of
LoaderInputReader
. Now you can resolve like this:也许你可以改变
这
利用了 解决带有名称的重载,在您的情况下,名称来自您的参数。
Perhaps you could change
with
This takes advantage of the Resolve overload that takes a name, where in your case the name comes from your argument.