DFLayoutObject.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. namespace InABox.Core
  4. {
  5. public abstract class DFLayoutObject : BaseObject
  6. {
  7. private Dictionary<string, object> _properties = new Dictionary<string, object>();
  8. protected void SetProperty(string name, object? value)
  9. {
  10. if (value == null)
  11. return;
  12. if (value.GetType().IsEnum)
  13. _properties[name] = value.ToString();
  14. else
  15. _properties[name] = value;
  16. }
  17. protected T GetProperty<T>(string key, T defaultvalue)
  18. {
  19. var result = defaultvalue;
  20. if (_properties.ContainsKey(key))
  21. try
  22. {
  23. if (typeof(T).IsEnum)
  24. result = (T)Enum.Parse(typeof(T), _properties[key].ToString());
  25. else
  26. result = (T)CoreUtils.ChangeType(_properties[key], typeof(T));
  27. }
  28. catch (Exception e)
  29. {
  30. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  31. }
  32. return result;
  33. }
  34. protected abstract void LoadProperties();
  35. protected abstract void SaveProperties();
  36. public void LoadFromString(string properties)
  37. {
  38. SetObserving(false);
  39. if (!string.IsNullOrEmpty(properties))
  40. _properties = Serialization.Deserialize<Dictionary<string, object>>(properties);
  41. else
  42. _properties = new Dictionary<string, object>();
  43. LoadProperties();
  44. SetObserving(true);
  45. }
  46. public string SaveToString()
  47. {
  48. _properties.Clear();
  49. SaveProperties();
  50. return Serialization.Serialize(_properties);
  51. }
  52. }
  53. }