3

I noticed that DynamicProxy objects can implement an IChangeProxyTarget interface, which allows you to do something like ((IChangeProxyTarget)myProxyObj).ChangeInvocationTarget(newTarget).

Is there a way to similarly change the mixin implementation on a DynamicProxy object? Obviously this is more complex and nuanced than changing the target in ways I have not fully thought through (due to varying interface implementations, multiplicity of mixins etc) but the concept is not entirely inconceivable.

Or lacking that, any ideas with regards achieving this, in a reasonably performant fashion? I have some theoretical ideas regarding hacking this in but it seems extremely, extremely convoluted:

  1. Define a MixinSwitcher class (and accompanying IMixinSwitcher interface) with Action<object, object> DoSwitch property
  2. Mix an instance of this in when creating DP object `MixinSwitcher mixinSwitcher = new MixinSwitcher(); proxyGenerationOptions.AddMixinInstance(mixinSwitcher);
  3. Create DP object var dpObj = proxyGenerator.Create... - ensure that IMixinSwitcher is added to the interfaces to implement
  4. Use reflection on to find the relevant mixin MemberInfo from dpObj.Gettype()
  5. Use System.Reflection.Emit to generate a fast setter for this property.
  6. Set mixinSwitcher.DoSwitch = (SRE setter method here)
  7. ((IMixinSwitcher)dpObj).DoSwitch(dpObj, newMixinValue)
  8. Profit... or brain melt?

The step 1 class can be genericised to allow it to target specific/multiple implementations; steps 4-5 cached for extra performance, and the general step 1 implementation could be cleaned up.

Even so I don't deny it is pretty mad - is there a better way?

fostandy
  • 4,282
  • 4
  • 37
  • 41

1 Answers1

0

No, that's not supported, mostly because no one came up with a good scenario where that would be useful. What is your scenario. Why do you want to be able to swap mixin targets?

Krzysztof Kozmic
  • 27,267
  • 12
  • 73
  • 115
  • Basically I was looking at whether I could [persist a mixin object with nhibernate](http://stackoverflow.com/questions/5366953/how-or-is-it-possible-to-persist-a-dynamicproxy-mixin-object-with-nhibernate) and wondered if I sort of could, if I built the object (with dummy mixin values) using a GetEntity interceptor and then switched them with the appropriate values when the object was PostLoaded. – fostandy Apr 10 '11 at 08:49