4

My code receives transparent proxy instead of original instance.

While this var type = obj.GetType(); yields the type of original class, the following code throws TargetException:

Object does not match target type

var value = property.GetValue(obj, null);

where property is one from the type.GetProperties()

Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
  • What type does `type` represent here? – DavidG Feb 09 '17 at 22:43
  • @DavidG obj- is an incoming object which appears to be a transparent proxy. But `GetType()` returns the type of a wrapped object. – Pavel Voronin Feb 09 '17 at 22:48
  • When you say "appears to be", what do you mean? – DavidG Feb 09 '17 at 22:49
  • @DavidG Real proxy of this transparent proxy is of type [InterceptingRealProxy](https://msdn.microsoft.com/en-us/library/microsoft.practices.unity.interceptionextension.interceptingrealproxy.aspx) – Pavel Voronin Feb 09 '17 at 22:50
  • I mean that before we enabled Unity Interception it was the registered dependency, and with enabled `TransparentProxyInterceptor` it is transparent proxy. We have additional teardown strategy which uses reflection. Not it throws error when tries to get property's value. Thus TransparentProxy is not so transparent in relation to reflection. – Pavel Voronin Feb 09 '17 at 22:53
  • 1
    I suspect this is some sort of restriction/bug in the implementation of `InterceptingRealProxy`. A quick test with a custom `RealProxy` shows that it is definitely possible to call `.GetType().GetProperty(...).GetValue(...)` on a transparent proxy, and have that method call (which is the property's getter) forwarded through the real proxy. – Jeroen Mostert Feb 10 '17 at 12:42
  • @JeroenMostert Thanks, that's fine. Will have a look at InterceptingRealProxy. – Pavel Voronin Feb 10 '17 at 12:53

0 Answers0