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

JsonReflectionUtilities

public static class JsonReflectionUtilities
Utility methods for reflection.
using Newtonsoft.Json.Serialization; using NJsonSchema.Infrastructure; using System; using System.Linq; using System.Reflection; namespace NJsonSchema.Generation { public static class JsonReflectionUtilities { private static readonly Lazy<CamelCasePropertyNamesContractResolver> CamelCaseResolverLazy = new Lazy<CamelCasePropertyNamesContractResolver>(); private static readonly Lazy<DefaultContractResolver> SnakeCaseResolverLazy = new Lazy<DefaultContractResolver>(() => new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() }); public static string GetPropertyName(MemberInfo property, PropertyNameHandling propertyNameHandling) { string name = ReflectionCache.GetPropertiesAndFields(property.DeclaringType).First((ReflectionCache.PropertyOrField p) => p.MemberInfo.Name == property.Name).GetName(); switch (propertyNameHandling) { case PropertyNameHandling.Default: return name; case PropertyNameHandling.CamelCase: return CamelCaseResolverLazy.Value.GetResolvedPropertyName(name); case PropertyNameHandling.SnakeCase: return SnakeCaseResolverLazy.Value.GetResolvedPropertyName(name); default: throw new NotSupportedException($"""{propertyNameHandling}"""); } } } }