Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# copy constructor generator

Tags:

c#

silverlight

I want to copy values from one object to another object. Something similar to pass by value but with assignment.

For example:

PushPin newValPushPin = oldPushPin; //I want to break the reference here.

I was told to write a copy constructor for this. But this class has a lot of properties, it will probably take an hour to write a copy constructor by hand.

  1. Is there a better way to assign an object to another object by value?
  2. If not, is there a copy constructor generator?

Note: ICloneable is not available in Silverlight.

like image 821
Shawn Mclean Avatar asked Mar 26 '10 08:03

Shawn Mclean


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

If you can mark the object that is to be cloned as Serializable then you can use in-memory serialization to create a copy. Check the following code, it has the advantage that it will work on other kinds of objects as well and that you don't have to change your copy constructor or copy code each time an property is added, removed or changed:

    class Program
    {
        static void Main(string[] args)
        {
            var foo = new Foo(10, "test", new Bar("Detail 1"), new Bar("Detail 2"));

            var clonedFoo = foo.Clone();

            Console.WriteLine("Id {0} Bar count {1}", clonedFoo.Id, clonedFoo.Bars.Count());
        }
    }

    public static class ClonerExtensions
    {
        public static TObject Clone<TObject>(this TObject toClone)
        {
            var formatter = new BinaryFormatter();

            using (var memoryStream = new MemoryStream())
            {
                formatter.Serialize(memoryStream, toClone);

                memoryStream.Position = 0;

                return (TObject) formatter.Deserialize(memoryStream);
            }
        }
    }

    [Serializable]
    public class Foo
    {
        public int Id { get; private set; }

        public string Name { get; private set; }

        public IEnumerable<Bar> Bars { get; private set; }

        public Foo(int id, string name, params Bar[] bars)
        {
            Id = id;
            Name = name;
            Bars = bars;
        }
    }

    [Serializable]
    public class Bar
    {
        public string Detail { get; private set; }

        public Bar(string detail)
        {
            Detail = detail;
        }
    }
like image 198
Thomas Avatar answered Oct 21 '22 15:10

Thomas


There is a protected member called "MemberwiseClone", you can write this in your class...

public MyClass Clone(){
   return (MyClass)this.MemberwiseClone();
}

then you can access..

MyClass newObject = oldObject.Clone();
like image 20
Akash Kava Avatar answered Oct 21 '22 16:10

Akash Kava