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

DynamicApis

static class DynamicApis
using System; using System.Reflection; using System.Text; using System.Xml.Linq; namespace NJsonSchema.Infrastructure { internal static class DynamicApis { private static readonly Type XPathExtensionsType; private static readonly Type FileType; private static readonly Type PathType; private static readonly Type WebClientType; public static bool SupportsFileApis { get { if ((object)FileType != null) return (object)PathType != null; return false; } } public static bool SupportsXPathApis => (object)XPathExtensionsType != null; public static bool SupportsWebClientApis => (object)WebClientType != null; static DynamicApis() { XPathExtensionsType = TryLoadType("System.Xml.XPath.Extensions, System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", "System.Xml.XPath.Extensions, System.Xml.XPath.XDocument"); FileType = TryLoadType("System.IO.File", "System.IO.File, System.IO.FileSystem"); PathType = TryLoadType("System.IO.Path", "System.IO.Path, System.IO.FileSystem"); WebClientType = Type.GetType("System.Net.WebClient, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); } public static string HttpGet(string url) { dynamic val = (IDisposable)Activator.CreateInstance(WebClientType); using ((IDisposable)val) return (string)val.DownloadString(url); } public static bool FileExists(string filePath) { 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) { return (string)FileType.GetRuntimeMethod("ReadAllText", new Type[2] { typeof(string), typeof(Encoding) }).Invoke(null, new object[2] { filePath, Encoding.UTF8 }); } public static string PathCombine(string path1, string path2) { return (string)PathType.GetRuntimeMethod("Combine", new Type[2] { typeof(string), typeof(string) }).Invoke(null, new object[2] { path1, path2 }); } public static string PathGetDirectoryName(string filePath) { return (string)PathType.GetRuntimeMethod("GetDirectoryName", new Type[1] { typeof(string) }).Invoke(null, new object[1] { filePath }); } public static object XPathEvaluate(XDocument document, string path) { return (string)XPathExtensionsType.GetRuntimeMethod("XPathEvaluate", new Type[2] { typeof(XDocument), typeof(string) }).Invoke(null, new object[2] { document, path }); } private static Type TryLoadType(params string[] typeNames) { try { for (int i = 0; i < typeNames.Length; i++) { Type type = Type.GetType(typeNames[i]); if ((object)type != null) return type; } } catch { return null; } return null; } } }