lundi 12 novembre 2018

Make object equal to another, internally

Given an object A, of type MyObj, and an object B also of the same type, what is the best way to define a method in MyObj which would take a MyObj argument and set the current object equal to that one.

Example:

class MyObj 
{ 
    /*lots of fields*/
    public void SetEqual(MyObj other)
    {
        /* What should go here, so that when the method ends, this is equal to other in every field OR they are ReferenceEqual */
    }
}

Currently the only way I can think of is reflection or manually setting each value equal (not refactor friendly) - there's no way to internally repoint this to other as this is immutable. Is there a more efficient and pragmatic way to do this (reflection is slow; manual is clunky), or not?





Aucun commentaire:

Enregistrer un commentaire