<PackageReference Include="NJsonSchema" Version="5.4.6138.24413" />

ReflectionExtensions

public static class 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.get_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().get_BaseType(); while ((object)baseType != null) { if (typeNameStyle == TypeNameStyle.Name && baseType.get_Name() == typeName) return true; if (typeNameStyle == TypeNameStyle.FullName && baseType.FullName == typeName) return true; baseType = baseType.GetTypeInfo().get_BaseType(); } return false; } public static Type GetEnumerableItemType(this Type type) { Type[] genericTypeArguments = type.GetGenericTypeArguments(); if (genericTypeArguments.Length != 0) return genericTypeArguments[0]; return type.GetElementType(); } public static Type[] GetGenericTypeArguments(this Type type) { Type[] genericTypeArguments = type.GenericTypeArguments; while ((object)type != null && (object)type != typeof(object) && genericTypeArguments.Length == 0) { type = type.GetTypeInfo().get_BaseType(); if ((object)type != null) genericTypeArguments = type.GenericTypeArguments; } return genericTypeArguments; } internal static string GetSafeTypeName(Type type) { if (type.IsConstructedGenericType) return type.get_Name().Split(new char[1] { '`' }).First() + "Of" + string.Join("And", type.GenericTypeArguments.Select(GetSafeTypeName)); return type.get_Name(); } } }