我想"装饰"我自己的对象的属性。比方说,我想知道调用getter的次数,setter花了多长时间,记录任何新的值...
我试图尽可能简化这一点。理想的解决方案可能使用属性,它可能如下所示:
public class MyClass
{
[CountGetterCalls]
[LogSettingValue]
public int SomeProperty;
}
我没有成功实现这个,作为一个解决方案,我尝试了一种属性包装器。但这远非完美。你的方法是什么?
public class MyClass
{
public MyClass()
{
SomeProperty = new PropertyWrapper<int>(new CountCallsPropertyDecorator<int>(new LogSettingPropertyDecorator<int>(new NullDecorator<int>())));
}
public PropertyWrapper<int> SomeProperty;
}
public interface IPropertyDecorator<T>
{
void OnGetValue();
void OnSetValue(T value);
}
public class PropertyWrapper<T>
{
private readonly IPropertyDecorator<T> _decorator;
private T _value;
public PropertyWrapper(IPropertyDecorator<T> decorator)
{
_decorator = decorator;
}
public T Value
{
get
{
_decorator.OnGetValue();
return _value;
}
set
{
_decorator.OnSetValue(value);
_value = value;
}
}
}
public class CountCallsPropertyDecorator<T> : IPropertyDecorator<T>
{
private readonly IPropertyDecorator<T> _decorator;
private int _nbGetCalled;
private int _nbSetCalled;
public int NbGetCalled { get { return _nbGetCalled; } }
public int NbSetCalled { get { return _nbSetCalled; } }
public CountCallsPropertyDecorator(IPropertyDecorator<T> decorator)
{
_decorator = decorator;
}
public void OnGetValue()
{
_decorator.OnGetValue();
_nbGetCalled++;
}
public void OnSetValue(T value)
{
_decorator.OnSetValue(value);
_nbSetCalled++;
}
}
public class LogSettingPropertyDecorator<T> : IPropertyDecorator<T>
{
private readonly IPropertyDecorator<T> _decorator;
public LogSettingPropertyDecorator(IPropertyDecorator<T> decorator)
{
_decorator = decorator;
}
public void OnGetValue() { _decorator.OnGetValue(); }
public void OnSetValue(T value)
{
_decorator.OnSetValue(value);
Console.WriteLine("Property called.");
}
}
public class NullDecorator<T> : IPropertyDecorator<T>
{
public void OnGetValue() { }
public void OnSetValue(T value) { }
}