我可以像这样获取构造函数参数的类型:
Type type = paramInfo.ParameterType;
现在我想从这种类型创建存根对象。有可能吗?我尝试使用自动固定装置:
public TObject Stub<TObject>()
{
Fixture fixture = new Fixture();
return fixture.Create<TObject>();
}
..但它不起作用:
Type type = parameterInfo.ParameterType;
var obj = Stub<type>();//Compile error! ("cannot resolve symbol type")
你能帮我一下吗?
汽车夹具does有一个非通用 API 来创建对象,尽管有点隐藏(按设计) https://autofixture.codeplex.com/workitem/4229:
var fixture = new Fixture();
var obj = new SpecimenContext(fixture).Resolve(type);
As the 博客文章 http://thomasardal.com/non-generic-createanonymous-method-for-autofixture/@meilke 链接指出,如果您发现自己经常需要这个,您可以将其封装在扩展方法中:
public object Create(this ISpecimenBuilder builder, Type type)
{
return new SpecimenContext(builder).Resolve(type);
}
这使您可以简单地执行以下操作:
var obj = fixture.Create(type);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)