No Copy Constructor support in .Net framework.

I have a base class with a lot of data members and I wanted to create a constructor in derived class, that will take a base class as a parameter and assign all data members values from the parameter:

class Derived: Base
    Derived(Base baseClass)
      //Assign all data

The function is almost like  Copy Constructor, so I tried to search Google for it.
There is a discussion on MBR IT/.NET 247 : how to implement a C# copy constructor .

where Jeffrey Tan from Microsoft Support suggested :
public extendeddatagrid(MyDataGrid obj)
this=obj.Clone() as DataGrid;

Unfortunately, it is not allowed to assign object to this in .Net.
I’ve also found a good article on -- Copy Constructors vs ICloneable. 

But .Net framework doesn’t have a facility to assign all members like MemberwiseClone Method does for copying.

So developer has to write code to assign all data members from passed object. It is quite annoying for big classes like I have.

The related topisc are discussed in article Copying, Cloning, and Marshalling in .NET

Update: See my post Implement Copy constructor using Serialization or Reflecton

posted @ Friday, May 25, 2007 12:46 PM

Comments on this entry:

# re: No Copy Constructor support in .Net framework.

Left by Tim Hibbard at 5/25/2007 1:40 PM
Why not use reflection?

Like this:

# re: No Copy Constructor support in .Net framework.

Left by Michael Freidgeim at 5/27/2007 2:12 AM
Thanks, Tim.
To use reflection is a good idea. It has some performance penalties, but in most cases they are minor. Your approach could save a lot of development/code maintenance time.

# re: No Copy Constructor support in .Net framework.

Left by web development company at 8/19/2009 1:21 PM
I agree reflection would be more useful in this case

Your comment:

(not displayed)


Live Comment Preview: