gpt4 book ai didi

.net - 温莎城堡3(CaSTLe Windsor 3),构造函数参数为字符串

转载 作者:行者123 更新时间:2023-12-04 13:14:38 33 4
gpt4 key购买 nike

我以前从未使用过Windsor,但曾经使用过其他DI框架,此刻我遇到了一个相当奇怪的问题。

我有一个工厂类,该工厂类在其构造函数中使用字符串,但是,每当尝试解析该对象时,我都会收到一条异常消息:

Handler for System.String was not found.

<Message>Handler for System.String was not found.</Message>
<StackTrace>at Castle.MicroKernel.Resolvers.DefaultDependencyResolver
.TryGetHandlerFromKernel(DependencyModel dependency, CreationContext context)
in d:\60b7fa65294e7792\src\Castle.Windsor\MicroKernel\Resolvers\DefaultDependencyResolver.cs:line 403
at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.ResolveCore(CreationContext context, ComponentModel model, DependencyModel dependency) in d:\60b7fa65294e7792\src\Castle.Windsor\MicroKernel\Resolvers\DefaultDependencyResolver.cs:line 270</StackTrace>
<Type>Castle.MicroKernel.Handlers.HandlerException</Type>
</InnerException>
<Message>Missing dependency.
Component SomeExampleFactory has a dependency on System.String, which could not be
resolved.
Make sure the dependency is correctly registered in the container as a service, or
provided as inline argument.</Message>

该类如下所示:
public interface IDummyFactory
{
void DoSomething();
}

public class DummyFactory : IDummyFactory
{
private string someString;

public DummyFactory(string someConstructorArg)
{
someString = someConstructorArg;
}
}

使用以下DI设置:
var someString = "some constructor arg";
_container.Register(Component.For<IDummyFactory>()
.ImplementedBy<DummyFactory>()
.DependsOn(someString));

我假设它正在尝试进行某种类型的转换或格式化,从而导致其爆炸,但是由于类型本身是字符串,并且变量以字符串形式传递...甚至可能是这种情况正在尝试映射该变量的类型,而不是变量内容,但是我对DI框架和该领域的文档了解得不够多

最佳答案

尝试调用DependsOn重载,该重载采用键/值对的IDictionary来指定依赖项:

_container.Register(
Component.For<IDummyFactory>()
.ImplementedBy<DummyFactory>()
.DependsOn(new Hashtable
{
{ "someConstructorArg", "some constructor arg" }
}));

关于.net - 温莎城堡3(CaSTLe Windsor 3),构造函数参数为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8295167/

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