AotHelper.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #region License
  2. // The MIT License (MIT)
  3. //
  4. // Copyright (c) 2016 SaladLab
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in all
  14. // copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. // SOFTWARE.
  23. #endregion
  24. using System;
  25. using System.Collections;
  26. using System.Collections.Generic;
  27. using Newtonsoft.Json.Serialization;
  28. namespace Newtonsoft.Json.Utilities
  29. {
  30. /// <summary>
  31. ///
  32. /// </summary>
  33. public static class AotHelper
  34. {
  35. /// <summary>
  36. /// Don't run action but let a compiler detect the code in action as an executable block.
  37. /// </summary>
  38. public static void Ensure(Action action)
  39. {
  40. if (IsFalse())
  41. {
  42. try
  43. {
  44. action();
  45. }
  46. catch (Exception e)
  47. {
  48. throw new InvalidOperationException("", e);
  49. }
  50. }
  51. }
  52. /// <summary>
  53. /// Ensure(() => new T());
  54. /// </summary>
  55. public static void EnsureType<T>() where T : new()
  56. {
  57. Ensure(() => new T());
  58. }
  59. /// <summary>
  60. /// Ensure generic list type can be (de)deserializable on AOT environment.
  61. /// </summary>
  62. /// <typeparam name="T">The type of elements in the list</typeparam>
  63. public static void EnsureList<T>()
  64. {
  65. Ensure(() =>
  66. {
  67. var a = new List<T>();
  68. var b = new HashSet<T>();
  69. // var c = new CollectionWrapper<T>((IList)a);
  70. // var d = new CollectionWrapper<T>((ICollection<T>)a);
  71. });
  72. }
  73. /// <summary>
  74. /// Ensure generic dictionary type can be (de)deserializable on AOT environment.
  75. /// </summary>
  76. /// <typeparam name="TKey">The type of the keys in the dictionary.</typeparam>
  77. /// <typeparam name="TValue">The type of the values in the dictionary.</typeparam>
  78. public static void EnsureDictionary<TKey, TValue>()
  79. {
  80. Ensure(() =>
  81. {
  82. var a = new Dictionary<TKey, TValue>();
  83. // var b = new DictionaryWrapper<TKey, TValue>((IDictionary)null);
  84. // var c = new DictionaryWrapper<TKey, TValue>((IDictionary<TKey, TValue>)null);
  85. // var d = new DefaultContractResolver.EnumerableDictionaryWrapper<TKey, TValue>((IDictionary<TKey, TValue>)null);
  86. });
  87. }
  88. private static bool s_alwaysFalse = DateTime.UtcNow.Year < 0;
  89. /// <summary>
  90. /// Always return false but compiler doesn't know it.
  91. /// </summary>
  92. /// <returns>False</returns>
  93. public static bool IsFalse()
  94. {
  95. return s_alwaysFalse;
  96. }
  97. }
  98. }