using System; using System.Collections.Generic; using System.Reflection; using System.Runtime.CompilerServices; namespace Utf8Json.Internal { internal static class ReflectionExtensions { public static bool IsNullable(this System.Reflection.TypeInfo type) { return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(System.Nullable<>); } public static bool IsPublic(this System.Reflection.TypeInfo type) { return type.IsPublic; } public static bool IsAnonymous(this System.Reflection.TypeInfo type) { return type.GetCustomAttribute() != null && type.Name.Contains("AnonymousType") && (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$")) && (type.Attributes & TypeAttributes.NotPublic) == TypeAttributes.NotPublic; } public static IEnumerable GetAllProperties(this Type type) { return GetAllPropertiesCore(type, new HashSet()); } static IEnumerable GetAllPropertiesCore(Type type, HashSet nameCheck) { foreach (var item in type.GetRuntimeProperties()) { if (nameCheck.Add(item.Name)) { yield return item; } } if (type.BaseType != null) { foreach (var item in GetAllPropertiesCore(type.BaseType, nameCheck)) { yield return item; } } } public static IEnumerable GetAllFields(this Type type) { return GetAllFieldsCore(type, new HashSet()); } static IEnumerable GetAllFieldsCore(Type type, HashSet nameCheck) { foreach (var item in type.GetRuntimeFields()) { if (nameCheck.Add(item.Name)) { yield return item; } } if (type.BaseType != null) { foreach (var item in GetAllFieldsCore(type.BaseType, nameCheck)) { yield return item; } } } #if NETSTANDARD public static bool IsConstructedGenericType(this System.Reflection.TypeInfo type) { return type.AsType().IsConstructedGenericType; } public static MethodInfo GetGetMethod(this PropertyInfo propInfo) { return propInfo.GetMethod; } public static MethodInfo GetSetMethod(this PropertyInfo propInfo) { return propInfo.SetMethod; } #endif } }