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 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");
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(HttpClientType);
using ((IDisposable)val) {
object val2 = val.GetAsync(url).GetAwaiter();
ICriticalNotifyCompletion awaiter;
INotifyCompletion awaiter2;
AsyncTaskMethodBuilder<string> asyncTaskMethodBuilder;
if ((byte)val2.IsCompleted != 0) {
dynamic val3 = val2.GetResult();
val3.EnsureSuccessStatusCode();
if (<>o__16.<>p__4 == null)
<>o__16.<>p__4 = CallSite<Func<CallSite, object, string>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.None, typeof(string), typeof(DynamicApis)));
Func<CallSite, object, string> target = <>o__16.<>p__4.Target;
CallSite<Func<CallSite, object, string>> <>p__ = <>o__16.<>p__4;
dynamic val4 = val3.Content.ReadAsStringAsync().GetAwaiter();
if ((byte)val4.IsCompleted != 0) {
val2 = val4.GetResult();
return target(<>p__, val2);
}
awaiter = (val4 as ICriticalNotifyCompletion);
if (awaiter == null) {
awaiter2 = (INotifyCompletion)val4;
asyncTaskMethodBuilder.AwaitOnCompleted(ref awaiter2, ref *(<HttpGetAsync>d__16*));
awaiter2 = null;
} else
asyncTaskMethodBuilder.AwaitUnsafeOnCompleted(ref awaiter, ref *(<HttpGetAsync>d__16*));
awaiter = null;
;
}
awaiter = (val2 as ICriticalNotifyCompletion);
if (awaiter == null) {
awaiter2 = (INotifyCompletion)val2;
asyncTaskMethodBuilder.AwaitOnCompleted(ref awaiter2, ref *(<HttpGetAsync>d__16*));
awaiter2 = null;
} else
asyncTaskMethodBuilder.AwaitUnsafeOnCompleted(ref awaiter, ref *(<HttpGetAsync>d__16*));
awaiter = null;
;
}
}
public static Task<string> DirectoryGetCurrentDirectoryAsync()
{
if (!SupportsDirectoryApis)
throw new NotSupportedException("The System.IO.Directory API is not available on this platform.");
return Task.Factory.StartNew(() => (string)DirectoryType.GetRuntimeMethod("GetCurrentDirectory", new Type[0]).Invoke(null, new object[0]));
}
public static Task<string[]> DirectoryGetFilesAsync(string directory, string filter)
{
if (!SupportsDirectoryApis)
throw new NotSupportedException("The System.IO.Directory API is not available on this platform.");
return Task.Factory.StartNew(() => (string[])DirectoryType.GetRuntimeMethod("GetFiles", new Type[2] {
typeof(string),
typeof(string)
}).Invoke(null, new object[2] {
directory,
filter
}));
}
public static Task DirectoryCreateDirectoryAsync(string directory)
{
if (!SupportsDirectoryApis)
throw new NotSupportedException("The System.IO.Directory API is not available on this platform.");
return Task.Factory.StartNew(() => DirectoryType.GetRuntimeMethod("CreateDirectory", new Type[1] {
typeof(string)
}).Invoke(null, new object[1] {
directory
}));
}
public static async Task<bool> DirectoryExistsAsync(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 await Task.Factory.StartNew(() => (bool)DirectoryType.GetRuntimeMethod("Exists", new Type[1] {
typeof(string)
}).Invoke(null, new object[1] {
filePath
}));
}
public static async Task<bool> FileExistsAsync(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 await Task.Factory.StartNew(() => (bool)FileType.GetRuntimeMethod("Exists", new Type[1] {
typeof(string)
}).Invoke(null, new object[1] {
filePath
}));
}
public static Task<string> FileReadAllTextAsync(string filePath)
{
if (!SupportsFileApis)
throw new NotSupportedException("The System.IO.File API is not available on this platform.");
return Task.Factory.StartNew(() => (string)FileType.GetRuntimeMethod("ReadAllText", new Type[2] {
typeof(string),
typeof(Encoding)
}).Invoke(null, new object[2] {
filePath,
Encoding.UTF8
}));
}
public static Task FileWriteAllTextAsync(string filePath, string text)
{
if (!SupportsFileApis)
throw new NotSupportedException("The System.IO.File API is not available on this platform.");
return Task.Factory.StartNew(() => 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)
{
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 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
});
}
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;
}
}
}