#region License // The MIT License (MIT) // // Copyright (c) 2016 SaladLab // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. #endregion using System; using System.Collections; using System.Collections.Generic; using LC.Newtonsoft.Json.Serialization; namespace LC.Newtonsoft.Json.Utilities { /// /// /// public static class AotHelper { /// /// Don't run action but let a compiler detect the code in action as an executable block. /// public static void Ensure(Action action) { if (IsFalse()) { try { action(); } catch (Exception e) { throw new InvalidOperationException("", e); } } } /// /// Ensure(() => new T()); /// public static void EnsureType() where T : new() { Ensure(() => new T()); } /// /// Ensure generic list type can be (de)deserializable on AOT environment. /// /// The type of elements in the list public static void EnsureList() { Ensure(() => { var a = new List(); var b = new HashSet(); var c = new CollectionWrapper((IList)a); var d = new CollectionWrapper((ICollection)a); }); } /// /// Ensure generic dictionary type can be (de)deserializable on AOT environment. /// /// The type of the keys in the dictionary. /// The type of the values in the dictionary. public static void EnsureDictionary() { Ensure(() => { var a = new Dictionary(); var b = new DictionaryWrapper((IDictionary)null); var c = new DictionaryWrapper((IDictionary)null); var d = new DefaultContractResolver.EnumerableDictionaryWrapper((IDictionary)null); }); } private static bool s_alwaysFalse = DateTime.UtcNow.Year < 0; /// /// Always return false but compiler doesn't know it. /// /// False public static bool IsFalse() { return s_alwaysFalse; } } }