6

Autofixture has the ability to create an instance of any type using the Fixture.Create<T>() method.

But I need to create a type at runtime. By default the non-generic Fixture.Create() expects a 'seed', which is not what I want.

How do I create an instance of a given type at runtime?

Jack Ukleja
  • 13,061
  • 11
  • 72
  • 113
  • 4
    Possible duplicate of [AutoFixture: how to CreateAnonymous from a System.Type](http://stackoverflow.com/questions/16546850/autofixture-how-to-createanonymous-from-a-system-type) – Mark Seemann Dec 23 '16 at 05:49

1 Answers1

10

By looking through the Autofixture source code I worked out I can do it like so:

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

Not sure if it's the best way, but it seems to work.

Jack Ukleja
  • 13,061
  • 11
  • 72
  • 113