Search code examples
c#dynamic.net-4.0expando

Intercepting dynamic property accessors .NET 4


Here's an interesting question (to me at least): Is it possible to intercept arbitrary property accessors (getters/setters) in an object, such that I can reroute that request elsewhere, such as to a private member object?

I'd love it if I could make it appear as though my outer object automatically has access to certain inner properties (i.e. everything except for a few exceptions) of an inner object that the caller doesn't know about, in effect making the outer "shell" object behave like the inner object, without having to manually create properties that map to each of the inner object's properties.

There's probably a pattern for that, but my guess is it involves manually wiring the outer properties to the inner properties.


Solution

  • My open source framework ImpromptuInterface (on nuget) has a lot of tools for you to do these kind of things. It's main feature is than it generates proxy's that implement and interface and forward the calls using the dlr.

    It's also got a lot of functions that make dlr calls interactively accessible. And some examples of DynamicObjects that give a starting place for making useful dynamic objects like ImpromptuForwarder.