Someta supports property interceptors. What this means is that when you decorate your property with an implementation of one or both of IPropertyGetInterceptor
and IPropertySetInterceptor
you can have your own code called instead. Both property gets and sets allow you to call the original implementation via a provided delegate.
This interface has one method:
object GetPropertyValue(PropertyInfo propertyInfo, object instance, Func<object> getter);
As you can see, your implementation is provided with everything you need to customize the behavior of the getter. If you don't want to call the original get, simply don't invoke getter
.
public void PropertyGetExample()
{
var testClass = new PropertyGetTestClass();
testClass.Value = 3;
Console.WriteLine(testClass.Value); // Prints 6
}
class PropertyGetTestClass
{
[PropertyGetInterceptor]
public int Value { get; set; }
}
[AttributeUsage(AttributeTargets.Property)]
class PropertyGetInterceptor : Attribute, IPropertyGetInterceptor
{
public object GetPropertyValue(PropertyInfo propertyInfo, object instance, Func<object> getter)
{
var currentValue = (int)getter();
return currentValue * 2;
}
}
This interface has one method:
void SetPropertyValue(PropertyInfo propertyInfo, object instance, object oldValue, object newValue, Action<object> setter);
As you can see, your implementation is provided with everything you need to customize the behavior of the setter. If you don't want to call the original set, simply don't invoke setter
.
public void PropertySetExample()
{
var testClass = new PropertySetTestClass();
testClass.Value = 2;
Console.WriteLine(testClass.Value); // Prints 4
}
class PropertySetTestClass
{
[PropertySetInterceptor]
public int Value { get; set; }
}
[AttributeUsage(AttributeTargets.Property)]
class PropertySetInterceptor : Attribute, IPropertySetInterceptor
{
public void SetPropertyValue(PropertyInfo propertyInfo, object instance, object oldValue, object newValue, Action<object> setter)
{
var value = (int)newValue;
value *= 2;
setter(value);
}
}