I am using a HttpRuntime.Cache to store a list of objects that will be accessing frequently across sessions.
I use the following line of code to get the item from the cache:
List<chartData_Type> _chartData =
(List<chartData_Type>)HttpRuntime.Cache.Get("rollingMonth");
But, unfortunately when I update the _chartData, it updates the cached item too.
How can I simply get a copy of the cached item?
That is the way which .NET works because Cache just reference to the pointer of
List. Don’t know whether youchartData_Typeis value type or reference type.If value type, it is easy to use:
But if reference type, it comes to complicated, you need to implement DeepCopy method for your class, then do
DeepCopyfor each object in list.DeepClonemethod:In order to use this method, class
chartData_Typemust be marked[Serializable]:So, you can do deep clone manually: