Created
May 18, 2013 22:33
-
-
Save chkn/5605979 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Runtime.Serialization.Formatters.Binary; | |
using Android.OS; | |
using Android.App; | |
using Android.Content; | |
public static class AndroidExtensions { | |
static BinaryFormatter binaryFormatter; | |
public static void PutObject (this Bundle bundle, string key, object val) | |
{ | |
if (binaryFormatter == null) | |
binaryFormatter = new BinaryFormatter (); | |
using (var stream = new MemoryStream ()) { | |
binaryFormatter.Serialize (stream, val); | |
bundle.PutByteArray (key, stream.ToArray ()); | |
} | |
} | |
public static object GetObject (this Bundle bundle, string key) | |
{ | |
if (binaryFormatter == null) | |
binaryFormatter = new BinaryFormatter (); | |
var array = bundle.GetByteArray (key); | |
if (array == null) | |
return null; | |
using (var stream = new MemoryStream (array, false)) | |
return binaryFormatter.Deserialize (stream); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment