<PackageReference Include="Namotion.Reflection" Version="3.0.1" />

PropertyReader<TObject, TValue>

sealed class PropertyReader<TObject, TValue> : IPropertyReader
using System; using System.Reflection; using System.Runtime.CompilerServices; namespace Namotion.Reflection { [System.Runtime.CompilerServices.NullableContext(2)] [System.Runtime.CompilerServices.Nullable(0)] internal sealed class PropertyReader<TObject, TValue> : IPropertyReader { [System.Runtime.CompilerServices.Nullable(1)] private readonly PropertyInfo _propertyInfo; private Func<TObject, TValue> _getter; [System.Runtime.CompilerServices.NullableContext(1)] public PropertyReader(PropertyInfo propertyInfo) { _propertyInfo = propertyInfo; MethodInfo getMethod = propertyInfo.GetMethod; _getter = ((getMethod != (MethodInfo)null) ? ((Func<TObject, TValue>)Delegate.CreateDelegate(typeof(Func<TObject, TValue>), null, getMethod)) : null); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public TValue GetValue(TObject obj) { if (_getter == null) return (TValue)_propertyInfo.GetValue(obj); return _getter(obj); } [MethodImpl(MethodImplOptions.AggressiveInlining)] object IPropertyReader.GetValue(object obj) { return GetValue((TObject)obj); } } }