iKenndac
iKenndac

Reputation: 18776

Dynamically overriding a method -or- observing when a method is called at runtime?

I'm primarily an Objective-C/Cocoa developer, but I'm trying to implement the Observer pattern in C#.NET, specifically mimicking the NSKeyValueObserving protocols and methodology.

I've gotten as far as mimicking NSKVO with manual support, as described in Apple's KVO Programming Guide (see http://tinyurl.com/nugolr). Since I'm writing the setValue:forKey: methods myself, I can implement auto KVO notification through there.

However, I'd like to somehow implement auto KVO on all properties by dynamically overriding them at runtime. For example, replacing Button.Title.set with:

set { 
    this.willChangeValueForKey("title");
    title = value;
    this.didChangeValueForKey("title");
}

So, this is my question:

How do I dynamically override a method or property at runtime in C#? I've gotten as far as getting and invoking methods and properties by name using Reflection.MethodInfo. Alternatively, can I observe the runtime and find out when a method is about to be/has been called?

Upvotes: 3

Views: 1842

Answers (5)

iKenndac
iKenndac

Reputation: 18776

After doing extensive research on this subject, it appears that I can't do exactly what I'd like to do with .NET in its current state.

  • PostSharp's method is done at compile time, meaning I can't dynamically insert my own implementations to methods.

  • Reflection.Emit allows me to do this dynamically, but it generates a new instance of the created subclass - I need to do this so it works with the original instance.

  • INotifyPropertyChanging and INotifyPropertyChanged would be perfect if any of the existing .NET classes actually used them.

... so, at the moment I'm a bit stuck. I've put a more detailed piece on what I'm doing and how I'm trying to achieve in a post on my blog. Here's hoping .NET 4.0's dynamic dispatch will help!

Upvotes: 0

jpbochi
jpbochi

Reputation: 4406

This does not solves the problem of having to add the tracking code dynamically, but can be interesting to read: Trackable Properties with Weak Events

With this stuff you are able to track changes to specific properties and it makes easier to implement INotifyPropertyChanged (i.e. track changes to all properties).

Upvotes: 0

LBushkin
LBushkin

Reputation: 131696

Dynamic metaprogramming and aspect oriented programming are not yet strongly supported in C#. What you can do, is look at a free tool called PostSharp - it allows supports weaving aspects into your code around properties and method calls quite easily.

You can implement the INotifyPropertyChanged interface (without postsharp) and it can be used in certain contexts to notify observers that a value of a property has changed. However, it still requires that each property actually broadcast the change notification - which generally requires it to be specifically coded to support that. Injecting change notification to existing code (without actually changing the source) is not an easy thing to do in straight-up C#. PostSharp (other other AOP/dynamic proxy libraries) make this sort of thing dramatically easier.

Upvotes: 1

Mauricio Scheffer
Mauricio Scheffer

Reputation: 99730

You're looking for INotifyPropertyChanged. You can dynamically implement that using PostSharp, Castle DynamicProxy or probably any other proxying library.

Upvotes: 0

AaronLS
AaronLS

Reputation: 38365

I'm not sure if you need to go down this road or not. But if you want to implement overrides of a method (i.e. generating new code for the method?) then it is possible with Emit. I would explore any other suggestions first before diving into those deep waters.

Upvotes: 0

Related Questions