Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Serializes the specified data, and returns an Url Token Encoded string
- /// </summary>
- public static string UrlEncode(object data) {
- if(data == null) {
- throw new ArgumentNullException("data");
- }
- BinaryFormatter formatter = new BinaryFormatter();
- byte[] dataBytes;
- /* Serialize the data to a byte array. */
- using(MemoryStream stream = new MemoryStream()) {
- formatter.Serialize(stream,data);
- dataBytes = stream.ToArray();
- }
- /* URL encode the result. */
- return HttpServerUtility.UrlTokenEncode(dataBytes);
- }
- /// <summary>
- /// Deserializes the Url Token Encoded string
- /// </summary>
- public static object UrlDecode(string value) {
- return UrlDecode<object>(value);
- }
- /// <summary>
- /// Deserializes the Url Token Encoded string
- /// </summary>
- public static T UrlDecode<T>(string value) {
- if(value == null) {
- throw new ArgumentNullException("value");
- }
- /* Decode the data. */
- byte[] decoded = HttpServerUtility.UrlTokenDecode(value);
- /* Reinstantiate the object instance. */
- BinaryFormatter formatter = new BinaryFormatter();
- object deserialized;
- using(MemoryStream stream = new MemoryStream(decoded)) {
- deserialized = formatter.Deserialize(stream);
- }
- return (T)deserialized;
- }
Advertisement
Add Comment
Please, Sign In to add comment