<PackageReference Include="NJsonSchema" Version="10.1.23" />

DynamicApis

public static class DynamicApis
Provides dynamic access to framework APIs.
using Microsoft.CSharp.RuntimeBinder; using System; using System.Reflection; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace NJsonSchema.Infrastructure { public static class DynamicApis { private static readonly Type XPathExtensionsType; private static readonly Type FileType; private static readonly Type DirectoryType; private static readonly Type PathType; private static readonly Type HttpClientHandlerType; private static readonly Type HttpClientType; public static bool SupportsFileApis => FileType != (Type)null; public static bool SupportsPathApis => PathType != (Type)null; public static bool SupportsDirectoryApis => DirectoryType != (Type)null; public static bool SupportsXPathApis => XPathExtensionsType != (Type)null; public static bool SupportsHttpClientApis => HttpClientType != (Type)null; static DynamicApis() { XPathExtensionsType = TryLoadType("System.Xml.XPath.Extensions, System.Xml.XPath.XDocument", "System.Xml.XPath.Extensions, System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); HttpClientHandlerType = TryLoadType("System.Net.Http.HttpClientHandler, System.Net.Http", "System.Net.Http.HttpClientHandler, System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"); HttpClientType = TryLoadType("System.Net.Http.HttpClient, System.Net.Http", "System.Net.Http.HttpClient, System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"); FileType = TryLoadType("System.IO.File, System.IO.FileSystem", "System.IO.File"); DirectoryType = TryLoadType("System.IO.Directory, System.IO.FileSystem", "System.IO.Directory"); PathType = TryLoadType("System.IO.Path, System.IO.FileSystem", "System.IO.Path"); } public unsafe static async Task<string> HttpGetAsync(string url) { if (!SupportsHttpClientApis) throw new NotSupportedException("The System.Net.Http.HttpClient API is not available on this platform."); dynamic val = (IDisposable)Activator.CreateInstance(HttpClientHandlerType); using ((IDisposable)val) { dynamic val2 = (IDisposable)Activator.CreateInstance(HttpClientType, val); using ((IDisposable)val2) { val.UseDefaultCredentials = true; object val3 = val2.GetAsync(url).ConfigureAwait(false).GetAwaiter(); ICriticalNotifyCompletion awaiter; INotifyCompletion awaiter2; AsyncTaskMethodBuilder<string> asyncTaskMethodBuilder; if ((byte)val3.IsCompleted != 0) { dynamic val4 = val3.GetResult(); val4.EnsureSuccessStatusCode(); if (<>o__17.<>p__7 == null) <>o__17.<>p__7 = CallSite<Func<CallSite, object, string>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.None, typeof(string), typeof(DynamicApis))); Func<CallSite, object, string> target = <>o__17.<>p__7.Target; CallSite<Func<CallSite, object, string>> <>p__ = <>o__17.<>p__7; dynamic val5 = val4.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter(); if ((byte)val5.IsCompleted != 0) { val3 = val5.GetResult(); return target(<>p__, val3); } awaiter = (val5 as ICriticalNotifyCompletion); if (awaiter == null) { awaiter2 = (INotifyCompletion)val5; asyncTaskMethodBuilder.AwaitOnCompleted(ref awaiter2, ref *(<HttpGetAsync>d__17*)); awaiter2 = null; } else asyncTaskMethodBuilder.AwaitUnsafeOnCompleted(ref awaiter, ref *(<HttpGetAsync>d__17*)); awaiter = null; ; } awaiter = (val3 as ICriticalNotifyCompletion); if (awaiter == null) { awaiter2 = (INotifyCompletion)val3; asyncTaskMethodBuilder.AwaitOnCompleted(ref awaiter2, ref *(<HttpGetAsync>d__17*)); awaiter2 = null; } else asyncTaskMethodBuilder.AwaitUnsafeOnCompleted(ref awaiter, ref *(<HttpGetAsync>d__17*)); awaiter = null; ; } } } public static string DirectoryGetCurrentDirectory() { if (!SupportsDirectoryApis) throw new NotSupportedException("The System.IO.Directory API is not available on this platform."); return (string)DirectoryType.GetRuntimeMethod("GetCurrentDirectory", new Type[0]).Invoke(null, new object[0]); } public static string[] DirectoryGetFiles(string directory, string filter) { if (!SupportsDirectoryApis) throw new NotSupportedException("The System.IO.Directory API is not available on this platform."); return (string[])DirectoryType.GetRuntimeMethod("GetFiles", new Type[2] { typeof(string), typeof(string) }).Invoke(null, new object[2] { directory, filter }); } public static void DirectoryCreateDirectory(string directory) { if (!SupportsDirectoryApis) throw new NotSupportedException("The System.IO.Directory API is not available on this platform."); DirectoryType.GetRuntimeMethod("CreateDirectory", new Type[1] { typeof(string) }).Invoke(null, new object[1] { directory }); } public static bool DirectoryExists(string filePath) { if (!SupportsDirectoryApis) throw new NotSupportedException("The System.IO.Directory API is not available on this platform."); if (string.IsNullOrEmpty(filePath)) return false; return (bool)DirectoryType.GetRuntimeMethod("Exists", new Type[1] { typeof(string) }).Invoke(null, new object[1] { filePath }); } public static bool FileExists(string filePath) { if (!SupportsFileApis) throw new NotSupportedException("The System.IO.File API is not available on this platform."); if (string.IsNullOrEmpty(filePath)) return false; return (bool)FileType.GetRuntimeMethod("Exists", new Type[1] { typeof(string) }).Invoke(null, new object[1] { filePath }); } public static string FileReadAllText(string filePath) { if (!SupportsFileApis) throw new NotSupportedException("The System.IO.File API is not available on this platform."); return (string)FileType.GetRuntimeMethod("ReadAllText", new Type[2] { typeof(string), typeof(Encoding) }).Invoke(null, new object[2] { filePath, Encoding.UTF8 }); } public static void FileWriteAllText(string filePath, string text) { if (!SupportsFileApis) throw new NotSupportedException("The System.IO.File API is not available on this platform."); FileType.GetRuntimeMethod("WriteAllText", new Type[2] { typeof(string), typeof(string) }).Invoke(null, new object[2] { filePath, text }); } public static string PathCombine(string path1, string path2) { if (!SupportsPathApis) throw new NotSupportedException("The System.IO.Path API is not available on this platform."); return (string)PathType.GetRuntimeMethod("Combine", new Type[2] { typeof(string), typeof(string) }).Invoke(null, new object[2] { path1, path2 }); } public static string GetFullPath(string path) { if (!SupportsPathApis) throw new NotSupportedException("The System.IO.Path API is not available on this platform."); return (string)PathType.GetRuntimeMethod("GetFullPath", new Type[1] { typeof(string) }).Invoke(null, new object[1] { path }); } public static string PathGetDirectoryName(string filePath) { if (!SupportsPathApis) throw new NotSupportedException("The System.IO.Path API is not available on this platform."); return (string)PathType.GetRuntimeMethod("GetDirectoryName", new Type[1] { typeof(string) }).Invoke(null, new object[1] { filePath }); } public static object XPathEvaluate(XDocument document, string path) { if (!SupportsXPathApis) throw new NotSupportedException("The System.Xml.XPath.Extensions API is not available on this platform."); return XPathExtensionsType.GetRuntimeMethod("XPathEvaluate", new Type[2] { typeof(XDocument), typeof(string) }).Invoke(null, new object[2] { document, path }); } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static Task<T> FromResult<T>(T result) { return Task.FromResult(result); } private static Type TryLoadType(params string[] typeNames) { foreach (string typeName in typeNames) { try { Type type = Type.GetType(typeName, false); if (type != (Type)null) return type; } catch { } } return null; } } }