Thursday, 15 November 2012

Deep clone of an object by rewinding the Serialization stream

/// <summary>
        /// Serializes this class to a json string.
        /// </summary>
        /// <returns>A JSON string reprsenting a SelectListItem.</returns>
        public string ToJson()
        {
            using (var memoryStream = new MemoryStream())
            {
                var dataContractJsonSerializer = new DataContractJsonSerializer(typeof(JsonSerializableSelectListItem));
                dataContractJsonSerializer.WriteObject(memoryStream, this);
                memoryStream.Position = 0;
                using (var streamReader = new StreamReader(memoryStream))
                {
                    return streamReader.ReadToEnd();
                }
            }
        }

No comments:

Post a Comment

How to by pass the Jaguary X-Type enter security pin screen after changing battery

How to bypass security code on Jaguar X-Type touchscreen Satellite Navigational units. Just hold the skip track, AMEM and PTY buttons ...