How to "really" down-cast a DynamicProxy back to its original type (to send over WCF)

OK, the situation is we have a class, PatientDto, and a DynamicProxy generated by Castle, PatientDtoProxy.

We're using this proxy in the Silverlight client, then want to send it back to the server via a WCF service call.

The WCF service Contract expects a PatientDto (ie not the proxy) and, as expected, blows up if you try to send anything else.

Essentially, we feel like we should be "casting" it back to a PatientDto to get things to work... but in reality, even if you cast the reference down to PatientDto, it doesn't change anything -- WCF still sees the object in memory as a PatientDtoProxy and blows up.

Obviously, doing a deep-copy into a new'ed up PatientDto is an option (and does work), but an unpleasant one. Any techniques we're just not thinking of?


What about using AutoMapper and mapping your proxy to a real PatientDto object. Or just manually mapping it yourself.

Just to add a more favourable alternative to mapping to a new object, you can just extract the underlying object.

I use a helper class to do this:

using Castle.DynamicProxy;

namespace Magna.Client.Common.Proxy
    public class ProxyDtoUtils
        public static T GetUnderlying<T>(T proxy)
            return ProxyUtil.IsProxy(proxy) ? (T)ProxyUtil.GetUnproxiedInstance(proxy) : proxy;

Need Your Help

Multiple windows, Windows Universal

windows-runtime visual-studio-2015 winrt-xaml windows-10

I would like to have a second, (and more) windows for my windows universal app to run on a PC. But to my surprise this does not seem easy. In WPF for example I could add a new item to my project and

How to add an event handler exactly once, if it doesn't exists

c# .net oop events

I need to add and remove event handlers on my collection change: