【问题标题】:Copy a dictionary object in ASP.Net在 ASP.Net 中复制字典对象
【发布时间】:2011-07-11 23:04:32
【问题描述】:

我的可序列化类在下面,我正在尝试让它正确复制。

    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 = ?;
        }
    }

如何正确复制 dict_Options、dictFilterPermissions 和 listAdvancedOptions?

【问题讨论】:

  • 我有过这个,对你有用吗?公共静态对象 DeepClone(object source) { MemoryStream m = new MemoryStream(); BinaryFormatter b = new BinaryFormatter(); b.序列化(m,源); m.位置 = 0;返回 b.Deserialize(m); }

标签: c# asp.net dictionary copy


【解决方案1】:

我不确定这是否是你想要的,但也许你想克隆一个字典/列表。 如果您需要的是克隆字典而不是其中的对象,因此您只能获得引用的副本,那么您可以使用字典的默认构造函数。 请参阅问题以获得进一步解释:What is the best way to clone/deep copy a .NET generic Dictionary<string, T>?

如果您需要深度克隆(注意这是一项繁重的操作),您必须自己实现它,但这并不难:通过 Dictionary 类继承,实现 ICloneable 并使用 where 子句确保类型用作键/值也实现 ICloneable。

阅读该问题中给出的解释这部分的答案

【讨论】:

    【解决方案2】:

    从 .NET 论坛的链接中在线找到此内容。

        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;
        }
    

    这似乎可以工作并像这样使用它:

          AppContext tempContext = new AppContext();
    
            tempContext = appContext.DeepClone();
    

    【讨论】:

      猜你喜欢
      • 2019-08-29
      • 1970-01-01
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2023-03-29
      • 2016-03-05
      相关资源
      最近更新 更多