LegacyExtensions
Extensions for ensuring code portability.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace AngleSharp.Extensions
{
internal static class LegacyExtensions
{
public static string ConvertFromUtf32(this int utf32)
{
return char.ConvertFromUtf32(utf32);
}
public static int ConvertToUtf32(this string s, int index)
{
return char.ConvertToUtf32(s, index);
}
public static Task InvokeAsync(this Action action)
{
return Task.Factory.StartNew(action);
}
public static Task Delay(this CancellationToken token, int timeout)
{
return TaskEx.Delay(Math.Max(timeout, 4), token);
}
public static Type GetTypeInfo(this Type type)
{
return type;
}
public static Type AsType(this Type type)
{
return type;
}
public static bool Implements<T>(this Type type)
{
return Enumerable.Contains(type.GetInterfaces(), typeof(T));
}
public static ConstructorInfo GetConstructor(this Type type)
{
return (from m in type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
where m.GetParameters().Length == 0
select m).FirstOrDefault();
}
public static ConstructorInfo GetDeclaredConstructor(this Type type)
{
return type.GetConstructors().First();
}
public static ConstructorInfo[] GetDeclaredConstructors(this Type type)
{
return type.GetConstructors().ToArray();
}
public static PropertyInfo GetDeclaredProperty(this Type type, string name)
{
return type.GetProperty(name);
}
public static FieldInfo GetDeclaredField(this Type type, string name)
{
return type.GetField(name);
}
public static MethodInfo GetRuntimeMethod(this Type type, string name, Type[] parameters)
{
return type.GetMethod(name, parameters);
}
public static T GetCustomAttribute<T>(this ICustomAttributeProvider provider, bool inherit = false) where T : Attribute
{
object[] customAttributes = provider.GetCustomAttributes(typeof(T), inherit);
if (customAttributes.Length != 0)
return (T)customAttributes[0];
return null;
}
public static Delegate CreateDelegate(this MethodInfo method, Type type, object target)
{
return Delegate.CreateDelegate(type, target, method);
}
public static Type[] GetGenericTypeArguments(this Type type)
{
return type.GetGenericArguments();
}
public static bool IsStructType(this Type type)
{
return type.IsValueType;
}
public static bool IsClassType(this Type type)
{
return type.IsClass;
}
public static bool IsGenericClass(this Type type)
{
return type.IsGenericType;
}
public static MethodInfo GetMethodInfo(this Delegate method)
{
return method.Method;
}
public static bool IsLazy(this Type serviceType)
{
if (serviceType.IsGenericClass())
return serviceType.GetGenericTypeDefinition() == typeof(Lazy<>);
return false;
}
public static MethodInfo GetPrivateMethod(this Type type, string name)
{
return type.GetMethod(name);
}
public static Assembly GetAssembly(this Type type)
{
return type.Assembly;
}
public static bool IsFunc(this Type serviceType)
{
if (serviceType.IsGenericClass())
return serviceType.GetGenericTypeDefinition() == typeof(Func<>);
return false;
}
public static bool IsFuncWithParameters(this Type serviceType)
{
if (!serviceType.IsGenericClass())
return false;
Type genericTypeDefinition = serviceType.GetGenericTypeDefinition();
if (!(genericTypeDefinition == typeof(Func<, >)) && !(genericTypeDefinition == typeof(Func<, , >)) && !(genericTypeDefinition == typeof(Func<, , , >)))
return genericTypeDefinition == typeof(Func<, , , , >);
return true;
}
public static bool IsListOfT(this Type serviceType)
{
if (serviceType.IsGenericClass())
return serviceType.GetGenericTypeDefinition() == typeof(IList<>);
return false;
}
public static bool IsEnumerableOfT(this Type serviceType)
{
if (serviceType.IsGenericClass())
return serviceType.GetGenericTypeDefinition() == typeof(IEnumerable<>);
return false;
}
public static bool IsClosedGeneric(this Type serviceType)
{
if (serviceType.IsGenericClass())
return !serviceType.IsGenericTypeDefinition;
return false;
}
public static bool IsAbstractClass(this Type type)
{
return type.IsAbstract;
}
public static bool IsNestedPrivateClass(this Type type)
{
return type.IsNestedPrivate;
}
public static bool IsCollectionOfT(this Type serviceType)
{
if (serviceType.IsGenericClass())
return serviceType.GetGenericTypeDefinition() == typeof(ICollection<>);
return false;
}
public static bool ContainsGenericClassParameters(this Type type)
{
return type.ContainsGenericParameters;
}
public static bool IsGenericClassDefinition(this Type type)
{
return type.IsGenericTypeDefinition;
}
public static Type GetBaseType(this Type type)
{
return type.BaseType;
}
public static object[] GetCustomAttributes(this Assembly assembly, Type attributeType)
{
return assembly.GetCustomAttributes(attributeType, false);
}
public static void SetValue(this PropertyInfo property, object obj, object value)
{
property.SetValue(obj, value, null);
}
}
}