SmartEnum
SmartEnum copied to clipboard
Optimize reflection dependency in SmartEnum List property for performance-sensitive scenarios
This PR optimizes the reflection dependency through strategic caching improvements:
SmartFlagEnum Optimization
- Added cached enum options: Introduced
static readonly Lazy<List<TEnum>> _enumOptionsfield to match the efficiency ofSmartEnum - Updated List property: Changed from rebuilding collections via
_fromName.Value.Values.ToList().AsReadOnly()to direct cached access - Eliminated repeated reflection calls: Updated
FromValue(),DeserializeValue(),TryFromValue(), andFromValueToString()methods to use cached data instead of callingGetAllOptions()repeatedly
Type-Level Reflection Caching
Enhanced TypeExtensions.GetFieldsOfType<T>() with ConcurrentDictionary<Type, object> caching to prevent repeated reflection operations on the same type across different instances.
Backward Compatibility
- No breaking changes to public APIs
- Maintains full backward compatibility
- Thread-safe implementation preserved
@ardalis Could you review this, please?