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

ReflectionExtensions

public static class ReflectionExtensions
Provides extension methods for reflection.
using NJsonSchema.Annotations; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace NJsonSchema.Infrastructure { public static class ReflectionExtensions { public static bool HasProperty(this object obj, string propertyName) { return (object)((obj != null) ? obj.GetType().GetRuntimeProperty(propertyName) : null) != null; } public static T TryGetPropertyValue<T>(this object obj, string propertyName, T defaultValue = default(T)) { PropertyInfo propertyInfo = (obj != null) ? obj.GetType().GetRuntimeProperty(propertyName) : null; if ((object)propertyInfo != null) return (T)propertyInfo.GetValue(obj); return defaultValue; } 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 Type FindCommonBaseType(this IEnumerable<Type> types) { Type baseType = types.First(); while ((object)baseType != typeof(object) && (object)baseType != null) { if (types.All((Type t) => baseType.GetTypeInfo().IsAssignableFrom(t.GetTypeInfo()))) return baseType; baseType = baseType.GetTypeInfo().get_BaseType(); } return typeof(object); } public static T TryGetIfAssignableTo<T>(this IEnumerable<T> attributes, string typeName, TypeNameStyle typeNameStyle = TypeNameStyle.FullName) { if (attributes == null) return default(T); 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) { JsonSchemaAttribute customAttribute = type.GetTypeInfo().GetCustomAttribute<JsonSchemaAttribute>(); if ((object)customAttribute?.ArrayItem != null) return customAttribute.ArrayItem; Type[] genericTypeArguments = type.GetGenericTypeArguments(); Type type2 = (genericTypeArguments.Length == 0) ? type.GetElementType() : genericTypeArguments[0]; if ((object)type2 == null) { foreach (Type implementedInterface in type.GetTypeInfo().ImplementedInterfaces) { type2 = implementedInterface.GetEnumerableItemType(); if ((object)type2 != null) return type2; } return type2; } return type2; } 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(); } } }