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

ContextualPropertyInfo

A property info with contextual information.
using System; using System.Reflection; using System.Runtime.CompilerServices; namespace Namotion.Reflection { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public class ContextualPropertyInfo : ContextualAccessorInfo { [System.Runtime.CompilerServices.Nullable(2)] private string _name; private bool? _canWrite; private bool? _canRead; [System.Runtime.CompilerServices.Nullable(2)] private IPropertyWriter _propertyWriter; [System.Runtime.CompilerServices.Nullable(2)] private IPropertyReader _propertyReader; public PropertyInfo PropertyInfo { get; } public override ContextualType AccessorType => PropertyType; public ContextualType PropertyType { get; set; } public override string Name => _name ?? (_name = PropertyInfo.Name); public override MemberInfo MemberInfo => PropertyInfo; public bool CanWrite { get { bool? canWrite = _canWrite; if (!canWrite.HasValue) { bool? nullable = _canWrite = PropertyInfo.CanWrite; return nullable.Value; } return canWrite.GetValueOrDefault(); } } public bool CanRead { get { bool? canRead = _canRead; if (!canRead.HasValue) { bool? nullable = _canRead = PropertyInfo.CanRead; return nullable.Value; } return canRead.GetValueOrDefault(); } } internal ContextualPropertyInfo(PropertyInfo propertyInfo, ref int nullableFlagsIndex, [System.Runtime.CompilerServices.Nullable(2)] byte[] nullableFlags) { PropertyInfo = propertyInfo; NullableFlagsSource[] customAttributeProviders = (!propertyInfo.DeclaringType.IsNested) ? new NullableFlagsSource[1] { NullableFlagsSource.Create(propertyInfo.DeclaringType, propertyInfo.DeclaringType.GetTypeInfo().Assembly) } : new NullableFlagsSource[2] { NullableFlagsSource.Create(propertyInfo.DeclaringType, null), NullableFlagsSource.Create(propertyInfo.DeclaringType.DeclaringType, propertyInfo.DeclaringType.GetTypeInfo().Assembly) }; PropertyType = new ContextualType(propertyInfo.PropertyType, this, null, ref nullableFlagsIndex, nullableFlags, customAttributeProviders); } [MethodImpl(MethodImplOptions.AggressiveInlining)] [System.Runtime.CompilerServices.NullableContext(2)] public override object GetValue(object obj) { if (_propertyReader == null) { lock (this) { if (_propertyReader == null) _propertyReader = PropertyReader.Create(PropertyInfo.DeclaringType, PropertyType.OriginalType, PropertyInfo); } } return _propertyReader.GetValue(obj); } [MethodImpl(MethodImplOptions.AggressiveInlining)] [System.Runtime.CompilerServices.NullableContext(2)] public override void SetValue(object obj, object value) { if (_propertyWriter == null) { lock (this) { if (_propertyWriter == null) _propertyWriter = PropertyWriter.Create(PropertyInfo.DeclaringType, PropertyType.OriginalType, PropertyInfo); } } _propertyWriter.SetValue(obj, value); } public override object[] GetCustomAttributes(Type attributeType, bool inherit) { return PropertyInfo.GetCustomAttributes(attributeType, inherit); } public override object[] GetCustomAttributes(bool inherit) { return PropertyInfo.GetCustomAttributes(inherit); } public override bool IsDefined(Type attributeType, bool inherit) { return PropertyInfo.IsDefined(attributeType, inherit); } } }