Gokul's Blog

Clone a Object in C#

Leave a comment

 public static void Clone(this Object Source, Object Destination)
        // exceptions
        if (Source == null)
            throw new ArgumentNullException("Source");
        if (Destination == null)
            throw new ArgumentNullException("Destination");
        if (Destination.GetType() != Source.GetType())
            throw new
        ArgumentException("Type mismatch");

        // copy each value over
        foreach (PropertyInfo pi in Source.GetType().GetProperties())
            if (pi.CanRead && pi.CanWrite)
                 pi.SetValue(Destination, pi.GetValue(Source, null), null);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s