Click here to Skip to main content
15,895,142 members
Articles / Programming Languages / C#

C# Object Cloning Machinery

Rate me:
Please Sign up or sign in to vote.
4.27/5 (8 votes)
19 Mar 2009CPOL2 min read 22K   393   33  
A class that clones your C# classes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rubenhak.Utils;

namespace TestApplication
{
    public class Person
    {
        private List<Person> _friends = new List<Person>();

        public string Firstname { get; set; }
        public string Lastname { get; set; }

        [Cloneable(CloneableState.Exclude)]
        [Cloneable(CloneableState.Include, "Friends")]
        public List<Person> Friends { get { return _friends; } }

        [Cloneable(CloneableState.Exclude)]
        public PersonManager Manager { get; set; }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(string.Format("[Person Name: {0} {1}, Friends: ", Firstname, Lastname));

            foreach (Person friend in _friends)
                sb.Append(friend.Firstname + ", ");

            sb.Append(string.Format("Manager: {0}]", Manager));
            
            return sb.ToString();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Independent Contractor
United States United States

Comments and Discussions