Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8701935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:31:26+00:00 2026-06-13T02:31:26+00:00

I have a struct e.g.: public struct Foo { public int Bar; public string

  • 0

I have a struct e.g.:

public struct Foo
{
    public int Bar;
    public string Baz;
}

and would like to create it in my unit tests using autofixture. I tried using the following:

IFixture fixture = new Fixture();
var f = fixture.CreateAnonymous<Foo>();

But this throws an AutoFixture was unable to create an instance error. Is it possible to auto create a struct with autofixture? How could this be done? Please note, that I am working on legacy code and thus need to deal with structs. 🙂

EDIT :

Changed

IFixture fixture = new Fixture().Customize(new AutoMoqCustomization());

To

IFixture fixture = new Fixture();

Since it wasn’t relevant for the question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:31:28+00:00Added an answer on June 13, 2026 at 2:31 am

    This is essentially an artefact of how the C# compiler treats value types. While the documentation seems to indicate otherwise, from the perspective of Reflection, the Foo struct has no public constructors.

    E.g. if you execute this code:

    var ctors = typeof(Foo).GetConstructors();
    

    the result is an empty array.

    However, this code compiles:

    var f = new Foo();
    

    so you could argue that AutoFixture should be able to create an instance of Foo.

    However, in the end, mutable structs are evil and should be avoided at all cost. A better option is to change the Foo implementation to this:

    public struct Foo
    {
        public Foo(int bar, string baz)
        {
            this.Bar = bar;
            this.Baz = baz;
        }
    
        public readonly int Bar;
        public readonly string Baz;
    }
    

    If you do this, not only do you now have a (more) correct value type, but AutoFixture is also able to create an instance without further modification.

    Thus, this is a pretty good example of the GOOS paradigm that you should listen to your tests. If they present friction, it might be feedback about your production code. In this case, this is exactly feedback that you’re about to shoot yourself in the foot because the value type implementation is flawed.

    P.S. Even if you ‘fix’ the Foo struct like outlined above, what’s the point of making it a struct instead of a class? It still contains a string (reference types) field, so even though the struct itself is going to live on the stack, the string field is still going to point to data in the heap.


    I’ve added this issue as a possible new feature for AutoFixture.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a struct like so public struct MyStruct { string StructConfig {
Suppose I have: struct Foo: public Bar { .... } Foo introduces no new
Given the following: struct Foo { int bar() const; }; struct IsEqual : public
Given the following: struct Foo { int bar() const; }; struct IsEqual : public
So I have following struct public struct Foo { public readonly int FirstLevel; public
Say I have a C# struct: struct Foo{ int mA; public int A {get
I have a struct that looks something like this: [StructLayout(LayoutKind.Sequential)] public struct in_addr {
I have the following struct private struct sData{ public int volume; public System.Timers.Timer aliveTimer;
I have a class class PCB { public: struct { string type; **linklist list;**//refer
I have this code (C#): using System.Collections.Generic; namespace ConsoleApplication1 { public struct Thing {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.