Question

I don't know if I am asking the impossible or not, I have tried to see what I could find via Google but perhaps I am not expressing what I am looking for in the right search terms. If it is out there please forgive me!

I was hoping to use a Custom Attribute to execute additional code when the set method of a property is executed.

Here is some pseudo code:

public class MyAttribute : System.Attribute{
     AttributedProperty.Get(Property p){
          Console.WriteLine("The value of the Property '{0}' has just been retrieved!", p.Name);
     }
     AttributedProperty.Set(Property p){
          Console.WriteLine("The value of the Property '{0}' has just been set!", p.Name);
     }
}

public class MyClass{
     private string _someProperty;
     [MyAttribute]
     public string SomeProperty{
           get{ return _someProperty;}
           set{ someProperty = value;}
     }

}

Rarely is something as simple as pseudo code so I am prepared to do a bit of work, I just need some direction if I could please :)

Thank you!

Was it helpful?

Solution

Attributes are not suitable for this manner,they just add meta information on your members and you can read them via Reflection. Instead you can use some thing like Castle.Proxy to have interceptor on your methods and properties.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top