1

My serializable class is below, and I'm trying to get it to copy correctly.

    public class AppContext : IXmlSerializable 
    {
        public bool autoGeneratedTitle = true;
        public bool truncateLabels = false;

        public IDictionary<string, OPTIONS> dict_Options = new Dictionary<string, OPTIONS>();
        // Advanced Options
        public List<string> listAdvancedOptions = new List<string>();

        public IDictionary<string, string> dictFilterPermissions = new Dictionary<string, string>();




        public class OPTIONS {
            public string subjectId = string.Empty;
            public string varNumber = string.Empty;
            public string varName = string.Empty;
            public string format = string.Empty;
            public string varLabel = string.Empty;

            public IDictionary<string, string> dictTagElements = new Dictionary<string, string>();
            public IDictionary<string, string> dictRefElements = new Dictionary<string, string>();
            ////TJM add includeZero
            public string includeZero = string.Empty;
        }


        public void CopyContext(AppContext copy)
        {

            autoGeneratedTitle = copy.autoGeneratedTitle;
            truncateLabels = copy.truncateLabels;

            dict_Options = ?;
            dictFilterPermissions = ?;
            listAdvancedOptions = ?;
        }
    }

How do I copy the dict_Options, dictFilterPermissions, and listAdvancedOptions correctly?

cdub
  • 24,555
  • 57
  • 174
  • 303
  • I had this once, is it useful to you? public static object DeepClone(object source) { MemoryStream m = new MemoryStream(); BinaryFormatter b = new BinaryFormatter(); b.Serialize(m, source); m.Position = 0; return b.Deserialize(m); } – Hector Sanchez Jul 11 '11 at 23:13

2 Answers2

0

I'm not sure if this is what you want, but maybe you want to clone a dictionary/list. If what you need is cloning the dictionary but not objects inside it, so you only gain a copy of references, then you can use the default constructor of a Dictionary. See the question for further explaination: What is the best way to clone/deep copy a .NET generic Dictionary<string, T>?

If you need a deep clone (care that is a heavy operation) you have to implement it by yourself, but it's not that hard: Inherit by Dictionary class, implement ICloneable and ensure with where clause that types used as key/value implement ICloneable too.

Read the answer given in that question that explain this part too

Community
  • 1
  • 1
Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147
-2

Found this online off of a link from a .NET forum.

    public virtual AppContext DeepClone()
    {
        //First do a shallow copy.
        AppContext returnData = (AppContext)this.MemberwiseClone();

        //Get the type.
        Type type = returnData.GetType();

        //Now get all the member variables.
        FieldInfo[] fieldInfoArray = type.GetFields();

        //Deepclone members that extend AppContext.
        //This will ensure we get everything we need.
        foreach (FieldInfo fieldInfo in fieldInfoArray)
        {
            //This gets the actual object in that field.
            object sourceFieldValue = fieldInfo.GetValue(this);

            //See if this member is AppContext
            if (sourceFieldValue is AppContext)
            {
                //If so, cast as a AppContext.
                AppContext sourceAppContext = (AppContext)sourceFieldValue;

                //Create a clone of it.
                AppContext clonedAppContext = sourceAppContext.DeepClone();

                //Within the cloned containig class.
                fieldInfo.SetValue(returnData, clonedAppContext);
            }
        }
        return returnData;
    }

This appears to work and use it like this:

      AppContext tempContext = new AppContext();

        tempContext = appContext.DeepClone();
cdub
  • 24,555
  • 57
  • 174
  • 303