ReflectionExtensions
Provides extension methods for reflection.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace NJsonSchema.Infrastructure
{
public static class ReflectionExtensions
{
public static T TryGetByObjectType<T>(this IEnumerable<T> attributes, string typeName, TypeNameStyle typeNameStyle = TypeNameStyle.FullName)
{
return attributes.FirstOrDefault((T a) => a.GetType().FullName == typeName);
}
public static T TryGetIfAssignableTo<T>(this IEnumerable<T> attributes, string typeName, TypeNameStyle typeNameStyle = TypeNameStyle.FullName)
{
return attributes.FirstOrDefault((T a) => a.GetType().IsAssignableTo(typeName, typeNameStyle));
}
public static bool IsAssignableTo(this Type type, string typeName, TypeNameStyle typeNameStyle)
{
if (typeNameStyle == TypeNameStyle.Name && type.Name == typeName)
return true;
if (typeNameStyle == TypeNameStyle.FullName && type.FullName == typeName)
return true;
return type.InheritsFrom(typeName, typeNameStyle);
}
public static bool InheritsFrom(this Type type, string typeName, TypeNameStyle typeNameStyle)
{
Type baseType = type.GetTypeInfo().BaseType;
while (baseType != (Type)null) {
if (typeNameStyle == TypeNameStyle.Name && baseType.Name == typeName)
return true;
if (typeNameStyle == TypeNameStyle.FullName && baseType.FullName == typeName)
return true;
baseType = baseType.GetTypeInfo().BaseType;
}
return false;
}
public static Type GetEnumerableItemType(this Type type)
{
Type[] genericTypeArguments = type.GetGenericTypeArguments();
Type type2 = (genericTypeArguments.Length == 0) ? type.GetElementType() : genericTypeArguments[0];
if (type2 == (Type)null) {
Type[] interfaces = type.GetTypeInfo().GetInterfaces();
for (int i = 0; i < interfaces.Length; i++) {
type2 = interfaces[i].GetEnumerableItemType();
if (type2 != (Type)null)
return type2;
}
}
return type2;
}
public static Type[] GetGenericTypeArguments(this Type type)
{
Type[] genericArguments = type.GetGenericArguments();
while (type != (Type)null && type != typeof(object) && genericArguments.Length == 0) {
type = type.GetTypeInfo().BaseType;
if (type != (Type)null)
genericArguments = type.GetGenericArguments();
}
return genericArguments;
}
internal static string GetSafeTypeName(Type type)
{
if (type.IsGenericType)
return type.Name.Split(new char[1] {
'`'
}).First() + "Of" + string.Join("And", type.GetGenericArguments().Select(GetSafeTypeName));
return type.Name;
}
internal static MethodInfo GetRuntimeMethod(this Type type, string name, Type[] types)
{
return type.GetMethod(name, types);
}
internal static PropertyInfo GetRuntimeProperty(this Type type, string name)
{
return type.GetProperty(name);
}
internal static FieldInfo GetDeclaredField(this Type type, string name)
{
return type.GetField(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
}
internal static PropertyInfo[] GetRuntimeProperties(this Type type)
{
return type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
}
internal static Type GetTypeInfo(this Type type)
{
return type;
}
internal static Attribute[] GetCustomAttributes(this FieldInfo fieldInfo, bool inherit = true)
{
return fieldInfo.GetCustomAttributes(inherit).OfType<Attribute>().ToArray();
}
internal static Attribute[] GetCustomAttributes(this Type type, bool inherit = true)
{
return type.GetCustomAttributes(inherit).OfType<Attribute>().ToArray();
}
internal static Attribute[] GetCustomAttributes(this PropertyInfo propertyInfo, bool inherit = true)
{
return propertyInfo.GetCustomAttributes(inherit).OfType<Attribute>().ToArray();
}
internal static T[] GetCustomAttributes<T>(this Type type, bool inherit = true) where T : Attribute
{
return type.GetCustomAttributes(inherit).OfType<T>().ToArray();
}
internal static T[] GetCustomAttributes<T>(this PropertyInfo propertyInfo, bool inherit = true) where T : Attribute
{
return propertyInfo.GetCustomAttributes(inherit).OfType<T>().ToArray();
}
internal static T GetCustomAttribute<T>(this Type type) where T : Attribute
{
return GetCustomAttributes(type, true).OfType<T>().FirstOrDefault();
}
internal static T GetCustomAttribute<T>(this PropertyInfo propertyInfo) where T : Attribute
{
return GetCustomAttributes(propertyInfo, true).OfType<T>().FirstOrDefault();
}
internal static object GetValue(this PropertyInfo propertyInfo, object obj)
{
return propertyInfo.GetValue(obj, null);
}
internal static void SetValue(this PropertyInfo propertyInfo, object obj, object value)
{
propertyInfo.SetValue(obj, value, null);
}
}
}