我有一个通用接口(interface):
public interface IRepository<T> { ... }
我有一个这样的实现:
public class Repository<T> {
public Repository<T>() { ... }
}
StructureMap (v 2.6.3) 配置如下:
For(typeof(IRepository<>)).Use(typeof(Repository<>));
当我尝试拉出 IRepository<Something>
时从 StructureMap 中,我得到一个 Repository<Something>
正如预期的那样。 万岁!
现在我添加了第二个构造函数,实现如下所示:
public class Repository<T> {
public Repository<T>() { ... }
public Repository<T>(string database) { ... }
}
当我尝试获取 IRepository<Something>
时现在,我得到一个异常,因为它默认尝试使用带有参数的新构造函数。 嘘!
如何更改我的 StructureMap 配置,使其知道使用无参数构造函数?
最佳答案
您可以通过使用 [DefaultConstructor] 属性标记您希望 StructureMap 使用的构造函数来实现此目的。正如您在 StructureMap documentation 上看到的那样, 默认是贪心的。
关于c# - StructureMap 和泛型 - 如何指定默认构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10602386/