我有一个要在单元测试中实例化的类:
public class Customer
{
internal Customer(Guid id) {
// initialize property
}
}
Run Code Online (Sandbox Code Playgroud)
如果我用一个new Customer()作品从另一个(单元测试)程序集中实例化测试类,因为我添加了[assembly: InternalsVisibleTo("MyProject.Tests")]
var sut = new Customer(Guid.NewGuid()); // works
Run Code Online (Sandbox Code Playgroud)
但是当我在另一个(unittest)程序集中设置一个autofac容器时
var builder = new ContainerBuilder();
builder.RegisterType<Customer>().AsSelf();
var container = builder.Build();
Run Code Online (Sandbox Code Playgroud)
我用autofac无法解决。
var theParam = new NamedParameter("id", Guid.NewGuid());
_sut = container.Resolve<Customer>(theParam); // throws exception
Run Code Online (Sandbox Code Playgroud)
我最好的猜测是内部构造函数不可用。但是,添加[assembly: InternalsVisibleTo("Autofac")]到另一个旁边并没有帮助。
Autofac抛出的异常是
Autofac.Core.DependencyResolutionException:
An error occurred during the activation of a particular registration. See the inner exception for details.
Registration: Activator = Customer (ReflectionActivator),
Services = [MyProject.Customer],
Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime,
Sharing = None,
Ownership = OwnedByLifetimeScope
---> No accessible constructors were found for the type 'MyProject.Customer'.
Run Code Online (Sandbox Code Playgroud)
Autofac不能处理internal构造函数吗?
小智 6
Autofac无法找到非公共构造函数,因为它使用DefaultConstructorFinder类,该类默认情况下仅搜索公共构造函数。
您必须像这样创建IConstructorFinder接口的自定义实现:
public class AllConstructorFinder : IConstructorFinder
{
private static readonly ConcurrentDictionary<Type, ConstructorInfo[]> Cache =
new ConcurrentDictionary<Type, ConstructorInfo[]>();
public ConstructorInfo[] FindConstructors(Type targetType)
{
var result = Cache.GetOrAdd(targetType,
t => t.GetTypeInfo().DeclaredConstructors.ToArray());
return result.Length > 0 ? result : throw new NoConstructorsFoundException(targetType);
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您必须FindConstructorsWith在类型注册中使用扩展方法:
builder.RegisterType<Customer>()
.FindConstructorsWith(new AllConstructorFinder())
.AsSelf();
Run Code Online (Sandbox Code Playgroud)
在InternalsVisibleToAttribute这种情况下不能帮助,因为它只会影响编译时间。
| 归档时间: |
|
| 查看次数: |
389 次 |
| 最近记录: |