PortableExtensions
Extensions for ensuring code portability.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace AngleSharp.Extensions
{
internal static class PortableExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ConvertFromUtf32(this int utf32)
{
return char.ConvertFromUtf32(utf32);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ConvertToUtf32(this string s, int index)
{
return char.ConvertToUtf32(s, index);
}
public static Task InvokeAsync(this Action action)
{
return Task.Run(action);
}
public static Task Delay(this CancellationToken token, int timeout)
{
return Task.Delay(Math.Max(timeout, 4), token);
}
public static ConstructorInfo GetDeclaredConstructor(this Type type)
{
return type.GetTypeInfo().DeclaredConstructors.First();
}
public static bool Implements<T>(this Type type)
{
return type.GetTypeInfo().ImplementedInterfaces.Contains(typeof(T));
}
public static ConstructorInfo[] GetDeclaredConstructors(this Type type)
{
return type.GetTypeInfo().DeclaredConstructors.ToArray();
}
public static Type[] GetGenericTypeArguments(this Type type)
{
return type.GetTypeInfo().get_GenericTypeArguments();
}
public static Type[] GetGenericTypeParameters(this Type type)
{
return type.GetTypeInfo().GenericTypeParameters;
}
public static Type[] GetGenericParameterConstraints(this Type type)
{
return type.GetTypeInfo().GetGenericParameterConstraints();
}
public static MethodInfo[] GetMethods(this Type type)
{
return type.GetTypeInfo().DeclaredMethods.ToArray();
}
public static PropertyInfo[] GetProperties(this Type type)
{
return type.GetRuntimeProperties().ToArray();
}
public static Type[] GetInterfaces(this Type type)
{
return type.GetTypeInfo().ImplementedInterfaces.ToArray();
}
public static ConstructorInfo[] GetConstructors(this Type type)
{
return type.GetTypeInfo().DeclaredConstructors.Where(delegate(ConstructorInfo c) {
if (c.IsPublic)
return !c.IsStatic;
return false;
}).ToArray();
}
public static MethodInfo GetPrivateMethod(this Type type, string name)
{
return GetMethod(type, name);
}
public static MethodInfo GetMethod(this Type type, string name)
{
return type.GetTypeInfo().GetDeclaredMethod(name);
}
public static bool IsAssignableFrom(this Type type, Type fromType)
{
return type.GetTypeInfo().IsAssignableFrom(fromType.GetTypeInfo());
}
public static bool IsDefined(this Type type, Type attributeType, bool inherit)
{
return CustomAttributeExtensions.IsDefined(type.GetTypeInfo(), attributeType, inherit);
}
public static PropertyInfo GetProperty(this Type type, string name)
{
return type.GetTypeInfo().GetDeclaredProperty(name);
}
public static bool IsStructType(this Type type)
{
return type.GetTypeInfo().get_IsValueType();
}
public static bool IsClassType(this Type type)
{
return type.GetTypeInfo().get_IsClass();
}
public static bool IsAbstractClass(this Type type)
{
return type.GetTypeInfo().get_IsAbstract();
}
public static bool IsNestedPrivateClass(this Type type)
{
return type.GetTypeInfo().get_IsNestedPrivate();
}
public static bool IsGenericClass(this Type type)
{
return type.GetTypeInfo().get_IsGenericType();
}
public static bool ContainsGenericClassParameters(this Type type)
{
return type.GetTypeInfo().get_ContainsGenericParameters();
}
public static Type GetBaseType(this Type type)
{
return type.GetTypeInfo().get_BaseType();
}
public static bool IsGenericClassDefinition(this Type type)
{
return type.GetTypeInfo().get_IsGenericTypeDefinition();
}
public static MethodInfo GetSetMethod(this PropertyInfo propertyInfo)
{
return propertyInfo.SetMethod;
}
public static MethodInfo GetGetMethod(this PropertyInfo propertyInfo)
{
return propertyInfo.GetMethod;
}
public static Type[] GetTypes(this Assembly assembly)
{
return (from t in assembly.DefinedTypes
select t.AsType()).ToArray();
}
public static Assembly GetAssembly(this Type type)
{
return type.GetTypeInfo().get_Assembly();
}
public static ConstructorInfo GetConstructor(this Type type)
{
return type.GetTypeInfo().DeclaredConstructors.Where(delegate(ConstructorInfo c) {
if (!c.IsStatic)
return c.GetParameters().Length == 0;
return false;
}).FirstOrDefault();
}
public static ConstructorInfo GetConstructor(this Type type, Type[] types)
{
return GetConstructors(type).FirstOrDefault((ConstructorInfo c) => (from p in c.GetParameters()
select p.ParameterType).SequenceEqual(types));
}
public static bool IsEnumerableOfT(this Type serviceType)
{
if (serviceType.IsGenericClass())
return (object)serviceType.GetGenericTypeDefinition() == typeof(IEnumerable<>);
return false;
}
public static bool IsListOfT(this Type serviceType)
{
if (serviceType.IsGenericClass())
return (object)serviceType.GetGenericTypeDefinition() == typeof(IList<>);
return false;
}
public static bool IsCollectionOfT(this Type serviceType)
{
if (serviceType.IsGenericClass())
return (object)serviceType.GetGenericTypeDefinition() == typeof(ICollection<>);
return false;
}
public static bool IsReadOnlyCollectionOfT(this Type serviceType)
{
if (serviceType.IsGenericClass())
return (object)serviceType.GetGenericTypeDefinition() == typeof(IReadOnlyCollection<>);
return false;
}
public static bool IsReadOnlyListOfT(this Type serviceType)
{
if (serviceType.IsGenericClass())
return (object)serviceType.GetGenericTypeDefinition() == typeof(IReadOnlyList<>);
return false;
}
public static bool IsLazy(this Type serviceType)
{
if (serviceType.IsGenericClass())
return (object)serviceType.GetGenericTypeDefinition() == typeof(Lazy<>);
return false;
}
public static bool IsFunc(this Type serviceType)
{
if (serviceType.IsGenericClass())
return (object)serviceType.GetGenericTypeDefinition() == typeof(Func<>);
return false;
}
public static bool IsFuncWithParameters(this Type serviceType)
{
if (!serviceType.IsGenericClass())
return false;
Type genericTypeDefinition = serviceType.GetGenericTypeDefinition();
if ((object)genericTypeDefinition != typeof(Func<, >) && (object)genericTypeDefinition != typeof(Func<, , >) && (object)genericTypeDefinition != typeof(Func<, , , >))
return (object)genericTypeDefinition == typeof(Func<, , , , >);
return true;
}
public static bool IsClosedGeneric(this Type serviceType)
{
if (serviceType.IsGenericClass())
return !serviceType.IsGenericClassDefinition();
return false;
}
public static bool IsGenericGetInstanceMethod(this MethodInfo m)
{
if (m.Name == "GetInstance")
return m.IsGenericMethodDefinition;
return false;
}
public static Type GetElementType(this Type type)
{
if (type.IsGenericClass() && type.GetGenericTypeArguments().Count() == 1)
return type.GetGenericTypeArguments()[0];
return type.GetElementType();
}
}
}