给定一个具有多个构造函数的类 - 如何告诉 Resolve 使用哪个构造函数?
考虑以下示例类:
public class Foo
{
public Foo() { }
public Foo(IBar bar)
{
Bar = bar;
}
public Foo(string name, IBar bar)
{
Bar = bar;
Name = name;
}
public IBar Bar { get; set; }
public string Name { get; set; }
}
如果我想使用 Resolve 创建类型为 Foo 的对象,那么 Resolve 将如何知道要使用哪个构造函数?我该如何告诉它使用正确的构造函数?假设我有一个已注册了 IBar 的容器-它会理解应该优先选择接受 IBar 参数的构造函数吗?如果我还指定了一个字符串-它会使用 (string, IBar) 构造函数吗?Foo foo = unityContainer.Resolve<Foo>();
请忽略这个事实:如果该类只有一个构造函数,那么可能会更容易...