EnumStringConverter.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*********************************************************************************
  2. *Author: OnClick
  3. *Version: 0.0.1
  4. *UnityVersion: 2018.3.11f1
  5. *Date: 2019-04-07
  6. *Description: IFramework
  7. *History: 2018.11--
  8. *********************************************************************************/
  9. using System;
  10. namespace IFramework.Serialization
  11. {
  12. #pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释
  13. public class EnumStringConverter<T> : StringConverter<T> where T : struct
  14. {
  15. public override string ConvertToString(T t)
  16. {
  17. if (typeof(T).IsEnum)
  18. {
  19. ulong ul;
  20. try
  21. {
  22. ul = Convert.ToUInt64(t as Enum);
  23. }
  24. catch (OverflowException)
  25. {
  26. unchecked
  27. {
  28. ul = (ulong)Convert.ToInt64(t as Enum);
  29. }
  30. }
  31. return ul.ToString();
  32. }
  33. else
  34. throw new Exception("This Type is Not Enum "+ typeof(T));
  35. }
  36. public override bool TryConvert(string self, out T result)
  37. {
  38. return Enum.TryParse(self, out result);
  39. }
  40. }
  41. #pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释
  42. }