简体   繁体   English

C# - 在 Struct 中强制子对象不变性

[英]C# - Enforce Child Object Immutability in Struct

I am trying to create an immutable struct.我正在尝试创建一个不可变的结构。 The problem is, it contains mutable objects inside.问题是,它里面包含可变对象。 Is it possible to make those immutable (without modifying their class)?是否可以使那些不可变(不修改它们的类)? For example ( https://dotnetfiddle.net/KTiTzB ):例如( https://dotnetfiddle.net/KTiTzB ):

public class IntHolder
{
    public int X
    {
        get;
        set;
    }
}

public struct ImmutableStruct
{
    public int ImmutableInt
    {
        get;
        private set;
    }

    public IntHolder MyIntHolder
    {
        get;
        private set;
    }

    public ImmutableStruct(int immutableInt, IntHolder myIntHolder): this ()
    {
        ImmutableInt = immutableInt;
        MyIntHolder = myIntHolder;
    }
}

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Immutability");
        IntHolder intHolder = new IntHolder();
        intHolder.X = 40;
        Console.WriteLine(intHolder.X == 40); // TRUE
        ImmutableStruct immStruct = new ImmutableStruct(10, intHolder);
        Console.WriteLine(immStruct.ImmutableInt == 10); // TRUE
        // immStruct.ImmutableInt = 4; // THIS DOESN'T WORK, AS EXPECTED. Hurray!
        // immStruct.MyIntHolder = new IntHolder(3); // ALSO DOESN'T WORK, GOOD!
        immStruct.MyIntHolder.X = 4; // how can I prevent this from working?
    }
}

Is there a way to make the IntHolder member immutable while leaving the IntHolder class unmodified?有没有办法在不修改 IntHolder 类的同时使 IntHolder 成员不可变?

It's actually a big question: how to handle nested objects in immutable data?这实际上是一个大问题:如何处理不可变数据中的嵌套对象?

  1. https://softwareengineering.stackexchange.com/questions/279580/how-to-make-complex-objects-immutable https://softwareengineering.stackexchange.com/questions/279580/how-to-make-complex-objects-immutable

  2. https://medium.freecodecamp.org/handling-state-in-react-four-immutable-approaches-to-consider-d1f5c00249d5 https://medium.freecodecamp.org/handling-state-in-react-four-immutable-approaches-to-think-d1f5c00249d5

  3. https://redux.js.org/recipes/structuringreducers/immutableupdatepatterns https://redux.js.org/recipes/structuringreducers/immutableupdatepatterns

In my opinion, It's worthy to create immutable children models to make sure the whole object immutable.在我看来,创建不可变的子模型以确保整个对象不可变是值得的。

See comments in code below:请参阅以下代码中的注释:

public class IntHolder
{
    public int X
    {
        get;
        set;
    }

    public ImmutableIntHolder ToImmutable()//convert itself to ImmutableIntHolder 
    {
        return new ImmutableIntHolder(X);
    }
}
public class ImmutableIntHolder
{
    public ImmutableIntHolder(int x)
    {
        X = x;
    }
    public int X
    {
        get;
        private set;
    }

    public IntHolder ToIntHolder() //convert it back to mutable IntHolder 
    {
        return new IntHolder()
        {
            X = this.X
        };
    }
}
public struct ImmutableStruct
{
    public int ImmutableInt
    {
        get;
        private set;
    }

    public ImmutableIntHolder IntHolder //use ImmutableIntHolder instead
    {
        get;
        private set;
    }

    public ImmutableStruct(int immutableInt, IntHolder myIntHolder) : this()
    {
        ImmutableInt = immutableInt;
        IntHolder = myIntHolder.ToImmutable(); // convert to immutable
    }
}

another option is:另一种选择是:

public class IntHolder
{
    public int X
    {
        get;
        set;
    }
}


public class ImmutableStruct //changed to class
{
    public int ImmutableInt
    {
        get;
        private set;
    }

    public ImmutableIntHolder IntHolder
    {
        get;
        private set;
    }

    public ImmutableStruct(int immutableInt, IntHolder myIntHolder) //: this()
    {
        ImmutableInt = immutableInt;
        IntHolder = new ImmutableIntHolder(myIntHolder); // convert here.
    }

    public class ImmutableIntHolder
    {
        public ImmutableIntHolder(IntHolder intHolder)
        {
            //map all properties
            X = intHolder.X;
        }
        public int X
        {
            get;
            private set;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM