PropertyWriter<TObject, TValue>
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 PropertyWriter<TObject, TValue> : IPropertyWriter
{
[System.Runtime.CompilerServices.Nullable(1)]
private readonly PropertyInfo _propertyInfo;
private Action<TObject, TValue> _setter;
[System.Runtime.CompilerServices.NullableContext(1)]
public PropertyWriter(PropertyInfo propertyInfo)
{
_propertyInfo = propertyInfo;
MethodInfo setMethod = propertyInfo.SetMethod;
_setter = ((setMethod != (MethodInfo)null) ? ((Action<TObject, TValue>)Delegate.CreateDelegate(typeof(Action<TObject, TValue>), null, setMethod)) : null);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetValue(TObject obj, TValue value)
{
if (_setter != null)
_setter(obj, value);
else
_propertyInfo.SetValue(obj, value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IPropertyWriter.SetValue(object obj, object value)
{
SetValue((TObject)obj, (TValue)value);
}
}
}