Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofixture: How to create instance of type without using generic method

Tags:

autofixture

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?

like image 243
Jack Ukleja Avatar asked May 14 '26 17:05

Jack Ukleja


1 Answers

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.

like image 186
Jack Ukleja Avatar answered May 19 '26 03:05

Jack Ukleja



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!