Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static T BinReadFromString<T>(string value)
- {
- try
- {
- if (string.IsNullOrEmpty(value))
- return default(T);
- byte[] bytes = Convert.FromBase64String(value);
- BinaryFormatter bf = new BinaryFormatter();
- using (MemoryStream stream = new MemoryStream(bytes))
- {
- return (T)new BinaryFormatter().Deserialize(stream);
- }
- }
- catch (Exception ex)
- {
- Tracks.StackTrace("Base64: {0}. Type: {1}. Error: {2}", value, typeof(T), ex.ToString());
- }
- return default(T);
- }
- public static T GetSerialized<T>(string key, bool newInstanceOnNull = true)
- {
- string value = PlayerPrefs.GetString(key, null);
- T result = SerializationUtils.BinReadFromString<T>(value);
- if (result == null && newInstanceOnNull)
- result = (T)Activator.CreateInstance(typeof(T));
- return result;
- }
- public T GetResult<T>(object obj)
- {
- return (T) obj;
- }
- public void Main()
- {
- var v = GetResult<int>("123");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement