|
|
|
@ -3,18 +3,23 @@ using RockLib.Reflection.Optimized;
|
|
|
|
|
|
|
|
|
|
namespace StabilityMatrix.Core.Extensions; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static class ObjectExtensions |
|
|
|
|
{ |
|
|
|
|
/// <summary> |
|
|
|
|
/// Cache of Types to named field getters |
|
|
|
|
/// </summary> |
|
|
|
|
private static readonly Dictionary<Type, Dictionary<string, Func<object, object>>> FieldGetterTypeCache = new(); |
|
|
|
|
private static readonly Dictionary< |
|
|
|
|
Type, |
|
|
|
|
Dictionary<string, Func<object, object>> |
|
|
|
|
> FieldGetterTypeCache = new(); |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// Cache of Types to named field setters |
|
|
|
|
/// </summary> |
|
|
|
|
private static readonly Dictionary<Type, Dictionary<string, Action<object, object>>> FieldSetterTypeCache = new(); |
|
|
|
|
private static readonly Dictionary< |
|
|
|
|
Type, |
|
|
|
|
Dictionary<string, Action<object, object>> |
|
|
|
|
> FieldSetterTypeCache = new(); |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// Get the value of a named private field from an object |
|
|
|
@ -27,10 +32,17 @@ public static class ObjectExtensions
|
|
|
|
|
if (!fieldGetterCache.TryGetValue(fieldName, out var fieldGetter)) |
|
|
|
|
{ |
|
|
|
|
// Get the field |
|
|
|
|
var field = obj.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); |
|
|
|
|
var field = obj.GetType() |
|
|
|
|
.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); |
|
|
|
|
// Try get from parent |
|
|
|
|
field ??= obj.GetType() |
|
|
|
|
.BaseType?.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); |
|
|
|
|
|
|
|
|
|
if (field is null) |
|
|
|
|
{ |
|
|
|
|
throw new ArgumentException($"Field {fieldName} not found on type {obj.GetType().Name}"); |
|
|
|
|
throw new ArgumentException( |
|
|
|
|
$"Field {fieldName} not found on type {obj.GetType().Name}" |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Create a getter for the field |
|
|
|
@ -54,13 +66,17 @@ public static class ObjectExtensions
|
|
|
|
|
if (!fieldSetterCache.TryGetValue(fieldName, out var fieldSetter)) |
|
|
|
|
{ |
|
|
|
|
// Get the field |
|
|
|
|
var field = obj.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); |
|
|
|
|
var field = obj.GetType() |
|
|
|
|
.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); |
|
|
|
|
// Try get from parent |
|
|
|
|
field ??= obj.GetType().BaseType?.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); |
|
|
|
|
field ??= obj.GetType() |
|
|
|
|
.BaseType?.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); |
|
|
|
|
|
|
|
|
|
if (field is null) |
|
|
|
|
{ |
|
|
|
|
throw new ArgumentException($"Field {fieldName} not found on type {obj.GetType().Name}"); |
|
|
|
|
throw new ArgumentException( |
|
|
|
|
$"Field {fieldName} not found on type {obj.GetType().Name}" |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Create a setter for the field |
|
|
|
@ -84,13 +100,17 @@ public static class ObjectExtensions
|
|
|
|
|
if (!fieldSetterCache.TryGetValue(fieldName, out var fieldSetter)) |
|
|
|
|
{ |
|
|
|
|
// Get the field |
|
|
|
|
var field = obj.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); |
|
|
|
|
var field = obj.GetType() |
|
|
|
|
.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); |
|
|
|
|
// Try get from parent |
|
|
|
|
field ??= obj.GetType().BaseType?.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); |
|
|
|
|
field ??= obj.GetType() |
|
|
|
|
.BaseType?.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); |
|
|
|
|
|
|
|
|
|
if (field is null) |
|
|
|
|
{ |
|
|
|
|
throw new ArgumentException($"Field {fieldName} not found on type {obj.GetType().Name}"); |
|
|
|
|
throw new ArgumentException( |
|
|
|
|
$"Field {fieldName} not found on type {obj.GetType().Name}" |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Create a setter for the field |
|
|
|
|