DFLayoutObject.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. if (!string.IsNullOrEmpty(properties))
  39. _properties = Serialization.Deserialize<Dictionary<string, object>>(properties);
  40. else
  41. _properties = new Dictionary<string, object>();
  42. LoadProperties();
  43. }
  44. public string SaveToString()
  45. {
  46. _properties.Clear();
  47. SaveProperties();
  48. return Serialization.Serialize(_properties);
  49. }
  50. }
  51. }