Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. public static T BinReadFromString<T>(string value)
  2. {
  3. try
  4. {
  5. if (string.IsNullOrEmpty(value))
  6. return default(T);
  7.  
  8. byte[] bytes = Convert.FromBase64String(value);
  9.  
  10. BinaryFormatter bf = new BinaryFormatter();
  11.  
  12. using (MemoryStream stream = new MemoryStream(bytes))
  13. {
  14. return (T)new BinaryFormatter().Deserialize(stream);
  15. }
  16. }
  17. catch (Exception ex)
  18. {
  19. Tracks.StackTrace("Base64: {0}. Type: {1}. Error: {2}", value, typeof(T), ex.ToString());
  20. }
  21.  
  22. return default(T);
  23. }
  24.  
  25.  
  26.  
  27. public static T GetSerialized<T>(string key, bool newInstanceOnNull = true)
  28. {
  29. string value = PlayerPrefs.GetString(key, null);
  30. T result = SerializationUtils.BinReadFromString<T>(value);
  31.  
  32. if (result == null && newInstanceOnNull)
  33. result = (T)Activator.CreateInstance(typeof(T));
  34.  
  35. return result;
  36. }
  37.  
  38. public T GetResult<T>(object obj)
  39. {
  40. return (T) obj;
  41. }
  42.  
  43. public void Main()
  44. {
  45. var v = GetResult<int>("123");
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement