I have a scenario where I will receive a dictionary<string, object>(); that can also be recursive though not always, but the main issue is that it contains lambdas.
as we implement a transaction system I need to clone it, which works fine until I hit the lambdas. I tried to move these to delegates but the error changes and still gives me runtime issue.
technically I can re inject the lambdas after the cloning, but don't know how to tell the DataContractSerializer to ignore these.
I also cannot remove the lambdas prior to the cloning as the original object still needs them if I cancel the transaction.
In your
Clone()method you can use the data contract surrogate functionality to replace allSystem.Delegateobjects in your serialization graph with a serializable type, such as a lookup in a dictionary of delegates that you build as you serialize. Then, as you deserialize, you could replace the deserialized serialization surrogates with the original delegates.The following does this:
Using the above
Clone()extension method, the following test will pass:Notes:
IDataContractSurrogateinterface has been replaced withISerializationSurrogateProvider.