Getting and setting properties
private object GetProperty(T instance, string propertyName)
=> GetterCache[propertyName].Invoke(instance);
public TValue GetProperty<TValue>(T instance, Expression<Func<T, TValue>> property)
=> (TValue) GetterCache[GetMemberInfo(property).Name](instance);
private void SetProperty(T instance, string propertyName, object value)
{
Action<T, object> setter;
if (SetterCache.TryGetValue(propertyName, out setter))
{
setter(instance, value);
}
else
{
throw new KeyNotFoundException(
$"a property setter with the name does not {propertyName} exist on {typeof(T).FullName}");
}
}
public void SetProperty<TValue>(T instance, Expression<Func<T, TValue>> property, TValue value)
=> SetterCache[GetMemberInfo(property).Name](instance, value);
These methods are implemented in a very inconsitant way. Only one checks if the property exists in the dictionary, the other ones will crash. There's also a lot of repetition. The entire logic should be in only one getter/setter and the other overloads should call the main method.