Is there a quick way to transfer all variables of one identical object to another in C #?

This is probably a simple question. Suppose I have an object called Users and it contains many protected variables.

Inside the Users class, I have a method that creates a temporary users object, does something with it, and if successful, transfers all the variables from the Users Temp object to the one I have.

Is there some quick way to pass all variables from one Users object to another Users object without doing so using C #?

this.FirstName = temp.FirstName;
this.LastName = temp.LastName;
........75 variables later......
this.FavoriteColor = temp.FavoriteColor

      

0


source to share


5 answers


The best approach is to implement the IClonable interface. But you will find that it doesn't save you a lot of work.



+3


source


You should check out cloning in C #.



Deep cloning objects

+3


source


I think serializing and then deserializing the object will create a new instance of the object. This should be identical to the first object.

+1


source


A better solution might be to move that this method is outside of your class and then just assign the temp user object to reference your main user object like this:

_User = tmpUser;

      

saving you 75 lines of code. Whenever I have a class instantiating itself inside one of its own methods, I always like to blink a couple of times and make sure I really need to do this.

0


source


There is always a reflection option. Something substantially similar to this:

public static void Copy(object source, object target)
    {
        foreach (System.Reflection.PropertyInfo pi in source.GetType().GetProperties())
        {
            System.Reflection.PropertyInfo tpi = target.GetType().GetProperty(pi.Name);
            if (tpi != null && tpi.PropertyType.IsAssignableFrom(pi.PropertyType))
            {
                tpi.SetValue(target, pi.GetValue(source, null), null);
            }

        }
    }

      

Doesn't require source and target to be in any way, something is always, just the name and IsAssignable check. This has interesting side effects if you use reference types anywhere, but for the situation you just described, this is not a bad option to learn.

class sourceTester
{
    public bool Hello { get; set; }
    public string World { get; set; }
    public int Foo { get; set; }
    public List<object> Bar { get; set; }
}

class targetTester
{
    public int Hello {get; set;}
    public string World { get; set; }
    public double Foo { get; set; }
    public List<object> Bar { get; set; }
}

static void Main(string[] args)
    {


        sourceTester src = new sourceTester { 
            Hello = true, 
            World = "Testing",
            Foo = 123,
            Bar = new List<object>()
        };

        targetTester tgt = new targetTester();

        Copy(src, tgt);

        //Immediate Window shows the following:
        //tgt.Hello
        //0
        //tgt.World
        //"Testing"
        //tgt.Foo
        //0.0
        //tgt.Bar
        //Count = 0
        //src.Bar.GetHashCode()
        //59129387
        //tgt.Bar.GetHashCode()
        //59129387
    }

      

0


source







All Articles