使用 Unity 改变构造函数注入的字符串参数

发布于 2024-12-17 09:23:54 字数 2005 浏览 0 评论 0原文

我的目标是改变字符串参数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半城柳色半声笛 2024-12-24 09:23:54

像这样的东西应该可以工作:

container
    .RegisterType<LoaderInputReader>(
        "us",
        new InjectionConstructor(
            @"\\ad1\accounting$\Xml\qb_us.xml",
            new ResolvedParameter<Company>(),
            new ResolvedParameter<eTargets>()));
container
    .RegisterType<LoaderInputReader>(
        "intl",
        new InjectionConstructor(
            @"\\ad1\accounting$\Xml\qb_intl.xml",
            new ResolvedParameter<Company>(),
            new ResolvedParameter<eTargets>()));

这为 LoaderInputReader 的每个注册命名。现在你可以这样解决:

var us = container.Resolve<LoaderInputReader>("us");
var intl = container.Resolve<LoaderInputReader>("intl");

Something like this ought to work:

container
    .RegisterType<LoaderInputReader>(
        "us",
        new InjectionConstructor(
            @"\\ad1\accounting$\Xml\qb_us.xml",
            new ResolvedParameter<Company>(),
            new ResolvedParameter<eTargets>()));
container
    .RegisterType<LoaderInputReader>(
        "intl",
        new InjectionConstructor(
            @"\\ad1\accounting$\Xml\qb_intl.xml",
            new ResolvedParameter<Company>(),
            new ResolvedParameter<eTargets>()));

This names each registration of LoaderInputReader. Now you can resolve like this:

var us = container.Resolve<LoaderInputReader>("us");
var intl = container.Resolve<LoaderInputReader>("intl");
人间☆小暴躁 2024-12-24 09:23:54

也许你可以改变

driver = Container.Resolve<LoaderDriver>(args[1]); // "us" or "intl"

driver = Container.Resolve<LoaderDriver>(Container.Resolve<string>(args[1]))

利用了 解决带有名称的重载,在您的情况下,名称来自您的参数。

Perhaps you could change

driver = Container.Resolve<LoaderDriver>(args[1]); // "us" or "intl"

with

driver = Container.Resolve<LoaderDriver>(Container.Resolve<string>(args[1]))

This takes advantage of the Resolve overload that takes a name, where in your case the name comes from your argument.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文