StringFormatter.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System.Text;
  2. namespace IFramework.Serialization
  3. {
  4. public abstract class StringFormatter
  5. {
  6. public abstract void ConvertToString(object t, StringBuilder builder);
  7. public abstract bool TryConvertObject(string str, out object result);
  8. }
  9. public abstract class StringFormatter<T> : StringFormatter
  10. {
  11. public abstract bool TryConvert(string self, out T result);
  12. public override bool TryConvertObject(string str, out object result)
  13. {
  14. T t;
  15. if (TryConvert(str, out t))
  16. {
  17. result = t;
  18. return true;
  19. }
  20. else
  21. {
  22. result = default(T);
  23. return false;
  24. }
  25. }
  26. public override void ConvertToString(object t, StringBuilder builder)
  27. {
  28. ConvertToString((T)t, builder);
  29. }
  30. public abstract void ConvertToString(T t, StringBuilder builder);
  31. protected T MakeDefault()
  32. {
  33. return default(T);
  34. }
  35. }
  36. }