Is there a way to intercept setters and getters in C#?

Not generally; a few options though;

  • inherit from ContextBoundObject – which does allow this, but at a performance cost
  • write an explicit property (i.e. with a backing field), and add a utility method call manually
  • look at compile-time weavers, such as PostSharp – generally by spotting an attribute or similar
  • look at runtime code generators, as offered by some DI/IoC tools (and some other “decorator” based tools) – which either decorate or subclass your object to add the extra code

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)