【问题标题】:Autofixture: How to create instance of type without using generic methodAutofixture:如何在不使用泛型方法的情况下创建类型实例
【发布时间】:2016-12-23 12:10:07
【问题描述】:

Autofixture 能够使用Fixture.Create<T>() 方法创建任何类型的实例。

但我需要在运行时创建一个类型。默认情况下,非泛型 Fixture.Create() 需要一个“种子”,这不是我想要的。

如何在运行时创建给定类型的实例?

【问题讨论】:

标签: autofixture


【解决方案1】:

通过查看我制定的 Autofixture 源代码,我可以这样做:

var fixture = new Fixture();
Type type = ... // My runtime type
var instance = fixture.Create(type, new SpecimenContext(fixture));

不确定这是否是最好的方法,但它似乎有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    相关资源
    最近更新 更多