Is it possible to create a custom attribute (MyAttribute) such that during run-time the get of Derived.MyProperty actually calls Base.GetProperty("MyProperty") and the set of Derived.MyProperty calls Base.SetProperty("MyProperty", value).
class Derived : Base
{
[MyAttribute]
string MyProperty { get; set;}
}
class Base
{
XmlDoc _xmldoc;
void SetProperty(string key, string value)
{
set key, value into _xmldoc;
}
string GetProperty(string key);
{
get key value from _xmldoc;
}
}
Here is the solution derived from the comments below. Thanks to all commenters.
public class WatchAttribute : HandlerAttribute
{
public override ICallHandler CreateHandler(IUnityContainer container)
{
return new WatchHandler();
}
}
public class WatchHandler : ICallHandler
{
public int Order { get; set; }
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
Console.WriteLine(string.Format("Method '{0}' on object '{1}' was invoked.",
return getNext()(input, getNext);
}
}
public class SomeAction : MarshalByRefObject
{
[Watch]
public string MyProperty1 { get; set; }
public string MyProperty { get; set; }
}
class Program
{
static void Main(string[] args)
{
SomeAction c = new SomeAction();
IUnityContainer container = new UnityContainer()
.AddNewExtension<Interception>();
container.Configure<Interception>()
.SetInterceptorFor<SomeAction>(new TransparentProxyInterceptor())
.AddPolicy("WatchAttribute Policy")
.AddMatchingRule(new PropertyMatchingRule("*", PropertyMatchingOption.GetOrSet));
container.RegisterInstance<SomeAction>(c);
c = container.Resolve<SomeAction>();
c.MyProperty1 = "Hello";
c.MyProperty = "Hello";
}
}