Search code examples
objectserializationcloning

How to make clone of static variables of static class in c#


I am working on different approaches of cloning of an objects in c# but currently I stuck with the simple one. I have a static class with static variables & I want to make an exact copy of one of my static variable.I have sketched my code structure below:

public static class RULE_SET
{
    public static bool IsdataValid;
    public static GCBRequest GCBData;

    public static T Clone<T>(this T source)
    {
        try
        {
            if (!typeof(T).IsSerializable)
            {                   
                throw new ArgumentException("The type must be serializable.", "source");
            }            
            if (Object.ReferenceEquals(source, null))
            {                   
                return default(T);
            }

            IFormatter formatter = new BinaryFormatter();
            Stream stream = new MemoryStream();
            using (stream)
            {
                formatter.Serialize(stream, source);
                stream.Seek(0, SeekOrigin.Begin);
                return (T)formatter.Deserialize(stream);
            }
        }
        catch (Exception ee) { return default(T); }
    }
}

[XmlRoot(ElementName = "GCBRequest")]
    public class GCBRequest
    {
        [XmlElement(ElementName = "PID")]
        public string PID { get; set; }
        [XmlElement(ElementName = "AID")]
        public string AID { get; set; }
        [XmlElement(ElementName = "CID")]
        public string CID { get; set; }
     }

//code to load the RULE_SET

    string strJsonRuleset = "{\r\n  \"GCdBINRequest\": {\r\n\"PID\": \"(?s).*#M#20\",\r\n\"AID\": \"(?s).*#O#10\",\r\n\"CID\": \"(?s).*#O#25\"\r\n  }\r\n}";
     public class RULE_SET_LOCAL 
    {
        public static GCBRequest GCBData;
    }
    //from other method
  RULE_SET_LOCAL objParentRuleSet = new RULE_SET_LOCAL(); 
  objParentRuleSet = JsonConvert.DeserializeObject<RULE_SET_LOCAL>(strJsonRuleset);
  RULE_SET.GCBData = objParentRuleSet.GCBData;

//Main Method from which I have to create a clone object

  Object objRuleset;
  objRuleset =  RULE_SET.GCBData.Clone();

  if(objRuleset == null)
  {
  ** stuck here**
   I don't know why Everytime I got the null object ?
  }
  // but I have use
  objRuleset = RULE_SET.GCBData;
  if(objRuleset != null)
  {
   ** Successfully reached **
   //But I can't do any operation on this object as it will effect the original one.
  } 

Guys, Do you have any solution/Suggestion ? Please help me to understand this , any help will be appreciated.

Thanks.


Solution

  • After searching a lot I got this method :

        public static T Clone<T>(this T source)
        {
            var serialized = JsonConvert.SerializeObject(source);
            return JsonConvert.DeserializeObject<T>(serialized);
        }
    

    & now I am able to get the clone by calling

     object objRuleset = RULE_SET.Clone<GCBRequest>(RULE_SET.GCBData);