DynamicApis
Provides dynamic access to framework APIs.
using Namotion.Reflection;
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
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");
}
[AsyncStateMachine(typeof(<HttpGetAsync>d__17))]
public static Task<string> HttpGetAsync(string url, CancellationToken cancellationToken)
{
<HttpGetAsync>d__17 stateMachine = default(<HttpGetAsync>d__17);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<string>.Create();
stateMachine.url = url;
stateMachine.cancellationToken = cancellationToken;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
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[] DirectoryGetDirectories(string directory)
{
if (!SupportsDirectoryApis)
throw new NotSupportedException("The System.IO.Directory API is not available on this platform.");
return (string[])DirectoryType.GetRuntimeMethod("GetDirectories", new Type[1] {
typeof(string)
}).Invoke(null, new object[1] {
directory
});
}
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 string DirectoryGetParent(string path)
{
if (!SupportsDirectoryApis)
throw new NotSupportedException("The System.IO.Directory API is not available on this platform.");
return DirectoryType.GetRuntimeMethod("GetParent", new Type[1] {
typeof(string)
}).Invoke(null, new object[1] {
path
}).TryGetPropertyValue<string>("FullName", null);
}
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 PathGetFileName(string filePath)
{
if (!SupportsPathApis)
throw new NotSupportedException("The System.IO.Path API is not available on this platform.");
return (string)PathType.GetRuntimeMethod("GetFileName", new Type[1] {
typeof(string)
}).Invoke(null, new object[1] {
filePath
});
}
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
});
}
public static string HandleSubdirectoryRelativeReferences(string fullPath, string jsonPath)
{
try {
if (!DirectoryExists(PathGetDirectoryName(fullPath))) {
string path = PathGetFileName(fullPath);
string text = PathGetDirectoryName(fullPath);
string path2 = text.Replace("\\", "/").Split(new char[1] {
'/'
}).Last();
if (!string.IsNullOrWhiteSpace(DirectoryGetParent(text))) {
string[] array = DirectoryGetDirectories(DirectoryGetParent(text));
for (int i = 0; i < array.Length; i++) {
string text2 = PathCombine(array[i], path2);
PathCombine(text2, path);
if (DirectoryExists(text2)) {
fullPath = PathCombine(text2, path);
break;
}
}
}
}
if (!FileExists(fullPath)) {
string text3 = PathGetDirectoryName(fullPath);
if (DirectoryExists(text3)) {
string path3 = PathGetFileName(fullPath);
string[] array2 = fullPath.Replace("\\", "/").Split(new char[1] {
'/'
});
string text4 = array2[array2.Length - 2];
string[] array = DirectoryGetDirectories(text3);
foreach (string path4 in array) {
string filePath = PathCombine(path4, path3);
if (FileExists(filePath) && FileReadAllText(filePath).Contains(jsonPath.Split(new char[1] {
'/'
}).Last())) {
fullPath = PathCombine(path4, path3);
break;
}
}
}
}
return fullPath;
} catch {
return fullPath;
}
}
[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;
}
}
}