2011-07-11 12 views
1

私のシリアライズ可能クラスは以下の通りですが、正しくコピーするようにしようとしています。私が正しくdict_Options、dictFilterPermissions、およびlistAdvancedOptionsをコピーするにはどうすればよい辞書オブジェクトをASP.Netにコピーします。

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

+0

、それはあなたに役立つでしょうか? public staticオブジェクトDeepClone(オブジェクトソース) { MemoryStream m = new MemoryStream(); BinaryFormatter b =新しいBinaryFormatter(); b.Serialize(m、source); m.Position = 0; return b.Deserialize(m); } –

+0

私はそれを試してみましょう。 – cdub

答えて

-2

を説明し、その質問に与えられた答えをお読みください。

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(); 
0

私はこれがあなたの望むものかどうかは分かりませんが、多分あなたはクローンの辞書/リストになりたいと思います。 必要なのは、辞書を複製するだけで、その中のオブジェクトは複製しないので、参照のコピーが得られます。次に、辞書のデフォルトのコンストラクタを使用できます。 What is the best way to clone/deep copy a .NET generic Dictionary<string, T>?

はあなたが自分でそれを実装する必要がありますが、それは難しいことではありません(重い操作でケア)深いクローンが必要な場合::Dictionaryクラスによって継承、ICloneableを実装し、確実 さらに、について説明のための質問を参照してください。キー/値として使用されるwhere節はICloneableを実装しています。

この部分に.NETフォーラムからのリンクのオフオンラインこれを見つけ、あまりにも

関連する問題