0

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";
  }
}
user481779
  • 1,071
  • 2
  • 14
  • 28
  • Why not just implement them to do that? – Mike Christensen Aug 18 '12 at 01:31
  • I am trying to simplify the development process for users of Base. Base is the class that is passed from node-to-node in a workflow and at each node the developer who coded the processor at the node can add data to the received Base object. I am trying to hide from the developer the added properties are actually contained in an xml document and also do it in a natural way for the developer. – user481779 Aug 18 '12 at 01:42
  • This question may solve your problem, if you ok with IL weaving [PostSharp aspect for property setters, calling generic method][1] [1]: http://stackoverflow.com/questions/246685/postsharp-aspect-for-property-setters-calling-generic-method – Ilya Ivanov Aug 18 '12 at 02:15

1 Answers1

1

use unity interception it will be better because it allow you to have a proxy that wrap you method you want and specify rule to filter method here an explanation: http://www.lm-tech.it/Blog/post/2011/10/18/How-to-use-the-Unity-Interception-Extension.aspx it allow you to make your process without setting any attribute and your developper will have only to follow a rule name for example (all properties that work with your xmldoc is prefixed with Xml_...

Hassan Boutougha
  • 3,871
  • 1
  • 17
  • 17
  • Thanks a lot. Unity allowed me to do exactly what I described above. It was a combination of TransparentProxyInterceptor, ICallHandler and PropertyMatchingRule that provided the solution. One negative is that to have an object's properties "intercepted" Unity creates the object via the Resolve method. I would prefer to have interception turned on or off on a previously created object - couldn't figure that out yet. Thanks again. – user481779 Aug 21 '12 at 13:53