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

  • Home
  • SEARCH
  • 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 617041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:23:30+00:00 2026-05-13T18:23:30+00:00

What tools/libraries exist that will take a struct and automatically generate an immutable wrapper

  • 0

What tools/libraries exist that will take a struct and automatically generate an immutable wrapper and also a “builder” class for incrementally building new instances?

Example input:

struct Foo
{
    public int apples;
    public int oranges;
    public Foo Clone() {return (Foo) base.MemberwiseClone();}
}

Example output:

public class ImmutableFoo // could probably be a struct
{
    private Foo snapshot;
    internal ImmutableFoo(Foo value) { this.snapshot = value; }
    public FooBuilder Builder() { return new FooBuilder(snapshot); }
    public int Apples { get { return snapshot.apples; } }
    public int Oranges { get { return snapshot.oranges; } }
}

public class FooBuilder
{
    private Foo state;

    public int Apples { get { return state.apples; } set { state.apples = value; } }
    public int Oranges { get { return state.oranges; } set { state.oranges = value; } }

    public FooBuilder() { }

    internal FooBuilder(Foo toCopy) { state = toCopy.Clone(); }

    public ImmutableFoo Build()
    {
        ImmutableFoo result = new ImmutableFoo(state);
        state = state.Clone();
        return result;
    }
}

Such a “tool” could be an IDE plugin or could generate the new class at run-time using reflection.

The example is in C# but I would be interested in a solution for any statically-typed OO language (Java, Scala, C++ etc.)

Desirable features:

  • Re-creates methods from the struct in the builder class
  • Re-creates nondestructive methods from the struct in the immutable class (esp. Equals() and GetHashCode() and any interface methods)
  • Also generates a IFooReader interface containing read-only properties for each struct member, implemented by both the immutable and the builder.
  • If a field’s class has an immutable equivalent, uses the immutable version in the immutable class (see also How do I create a builder in C# for an object that has properties that are referenc types?) e.g. List -> ReadOnlyCollection or similar.
  • Alternatively take the builder class as input (where the builder uses automatic properties instead of delegating to a struct.)
  • Does not require the Clone method to be predefined

“You should not use a tool like this because…” answers are also welcome.

  • 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-05-13T18:23:30+00:00Added an answer on May 13, 2026 at 6:23 pm

    Here are four possible solutions.

    1) Use CodeDOM to generate C# or VB code. This would also allow you to use visual studio extensions to generate your code in designer files. Similar to some of the built in tools that visual studio already offers – like the ones that generate wrappers for web service calls etc. Unfortunately I don’t know much about extending Visual Studio.

    • Pros – You can generate source prior to building. This makes it easier to write code against the generated types from any assembly.
    • Cons – Not language agnostic. You’re stuck with the languages that are supported.

    2) Use the Mono.Cecil library to analyze your assembly post-build. You can then re-write the assembly with the new types included.

    • Pros – Language agnostic.
    • Cons – If you add the types to same assembly in which your structs are defined you won’t be able to write code against the generated immutable struct types in the same assembly. If you put the generated types in a new assembly then this is possible.

    3) Use PostSharp. I don’t know as much about this library so you might not be able to add new types to your assembly but I know you can inject IL into methods. It also has a lot of nice stuff that makes it easy to do this with attributes. So you could do this –

    [GenerateImmutable]
    struct Foo
    {
        public int apples;
        public int oranges;
        public Foo Clone() {return (Foo) base.MemberwiseClone();}
    }
    
    • Pros – Language agnostic, AOP is easier to do in PostSharp.
    • Cons – Same as with Mono.Cecil and also not sure if you can generate new types using PostSharp.

    4) Use built in Reflection.Emit libraries to generate a new assembly with your immutable types.

    • Pros – Language agnostic, No 3rd party stuff.
    • Cons – Must put generated types in new assemblies. Can’t add them to the same assembly that the original type is in.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 372k
  • Answers 372k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here was the problem: As I said previously I have… May 14, 2026 at 7:21 pm
  • Editorial Team
    Editorial Team added an answer while ($row = mysql_fetch_object($result)){ // echo data echo $row->text; }… May 14, 2026 at 7:21 pm
  • Editorial Team
    Editorial Team added an answer You don't ever want to use floating point formats (such… May 14, 2026 at 7:21 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.