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

DynamicApis

public static class DynamicApis
Provides dynamic access to framework APIs.
using System; using System.Text; 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 WebClientType; public static bool SupportsFileApis { get { if (FileType != (Type)null) return PathType != (Type)null; return false; } } public static bool SupportsXPathApis => XPathExtensionsType != (Type)null; public static bool SupportsWebClientApis => WebClientType != (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"); 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"); WebClientType = TryLoadType("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 string DirectoryGetCurrentDirectory() { return (string)DirectoryType.GetRuntimeMethod("GetCurrentDirectory", new Type[0]).Invoke(null, new object[0]); } public static string[] DirectoryGetFiles(string directory, string filter) { 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) { DirectoryType.GetRuntimeMethod("CreateDirectory", new Type[1] { typeof(string) }).Invoke(null, new object[1] { directory }); } public static bool DirectoryExists(string filePath) { 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 (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 void FileWriteAllText(string filePath, string text) { FileType.GetRuntimeMethod("WriteAllText", new Type[3] { typeof(string), typeof(string), typeof(Encoding) }).Invoke(null, new object[3] { filePath, text, 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 string 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) { foreach (string typeName in typeNames) { try { Type type = Type.GetType(typeName, false); if (type != (Type)null) return type; } catch { } } return null; } } }