This is just Jetbrains formatting and code syntax styling

This commit is contained in:
CWX
2025-02-05 06:56:51 +00:00
parent ce78a2231e
commit f648f42721
382 changed files with 6348 additions and 10422 deletions
+62 -69
View File
@@ -1,81 +1,74 @@
using System.Reflection;
using System.Text.Json;
namespace SptCommon.Extensions
namespace SptCommon.Extensions;
public static class ObjectExtensions
{
public static class ObjectExtensions
private static readonly Dictionary<Type, Dictionary<string, PropertyInfo>> _indexedProperties = new();
private static readonly object _indexedPropertiesLockObject = new();
private static bool TryGetCachedProperty(Type type, string key, out PropertyInfo cachedProperty)
{
private static readonly Dictionary<Type, Dictionary<string, PropertyInfo>> _indexedProperties = new();
private static readonly object _indexedPropertiesLockObject = new();
private static bool TryGetCachedProperty(Type type, string key, out PropertyInfo cachedProperty)
lock (_indexedPropertiesLockObject)
{
lock (_indexedPropertiesLockObject)
if (!_indexedProperties.TryGetValue(type, out var properties))
{
if (!_indexedProperties.TryGetValue(type, out var properties))
{
properties = type.GetProperties().ToDictionary(prop => prop.GetJsonName(), prop => prop);
_indexedProperties.Add(type, properties);
}
return properties.TryGetValue(key, out cachedProperty);
properties = type.GetProperties().ToDictionary(prop => prop.GetJsonName(), prop => prop);
_indexedProperties.Add(type, properties);
}
}
/// <summary>
/// CARE WHEN USING THIS, THIS IS TO GET PROP ON A TYPE
/// </summary>
/// <param name="obj"></param>
/// <param name="key"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static bool ContainsJsonProp<T>(this object? obj, T key)
{
if (obj == null)
{
throw new ArgumentNullException(nameof(obj));
}
return TryGetCachedProperty(obj.GetType(), key.ToString(), out _);
}
public static T? GetByJsonProp<T>(this object? obj, string? toLower)
{
ArgumentNullException.ThrowIfNull(obj);
ArgumentNullException.ThrowIfNull(toLower);
if (!TryGetCachedProperty(obj.GetType(), toLower, out var cachedProperty))
return default;
return (T?)cachedProperty.GetValue(obj);
}
public static List<object> GetAllPropValuesAsList(this object? obj)
{
ArgumentNullException.ThrowIfNull(obj);
var list = obj.GetType().GetProperties();
var result = new List<object>();
foreach (var prop in list)
{
result.Add(prop.GetValue(obj));
}
return result;
}
public static Dictionary<string, object?> GetAllPropsAsDict(this object? obj)
{
var props = obj.GetType().GetProperties();
return props.ToDictionary(prop => prop.Name, prop => prop.GetValue(obj));
}
public static T ToObject<T>(this JsonElement element)
{
var json = element.GetRawText();
return JsonSerializer.Deserialize<T>(json);
return properties.TryGetValue(key, out cachedProperty);
}
}
/// <summary>
/// CARE WHEN USING THIS, THIS IS TO GET PROP ON A TYPE
/// </summary>
/// <param name="obj"></param>
/// <param name="key"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static bool ContainsJsonProp<T>(this object? obj, T key)
{
if (obj == null) throw new ArgumentNullException(nameof(obj));
return TryGetCachedProperty(obj.GetType(), key.ToString(), out _);
}
public static T? GetByJsonProp<T>(this object? obj, string? toLower)
{
ArgumentNullException.ThrowIfNull(obj);
ArgumentNullException.ThrowIfNull(toLower);
if (!TryGetCachedProperty(obj.GetType(), toLower, out var cachedProperty))
return default;
return (T?)cachedProperty.GetValue(obj);
}
public static List<object> GetAllPropValuesAsList(this object? obj)
{
ArgumentNullException.ThrowIfNull(obj);
var list = obj.GetType().GetProperties();
var result = new List<object>();
foreach (var prop in list) result.Add(prop.GetValue(obj));
return result;
}
public static Dictionary<string, object?> GetAllPropsAsDict(this object? obj)
{
var props = obj.GetType().GetProperties();
return props.ToDictionary(prop => prop.Name, prop => prop.GetValue(obj));
}
public static T ToObject<T>(this JsonElement element)
{
var json = element.GetRawText();
return JsonSerializer.Deserialize<T>(json);
}
}