Getting underlying type of a proxy object

Hadi Eskandari picture Hadi Eskandari · Sep 12, 2009 · Viewed 7.9k times · Source

I'm using Castle DynamicProxy and my ViewModels are a proxy, something like this:

namespace MyApplication.ViewModels
{
   public class MyViewModel : BaseViewModel, IMyViewModel
   {
   }
}

a proxy of my viewmodel looks like this though:

{Name = "IRootViewModelProxyffecb133f590422098ca7c0ac13b8f98" FullName = "IRootViewModelProxyffecb133f590422098ca7c0ac13b8f98"}

I want to get the actual type or namespace of the actual type that is being proxied. Is there any way to do this? I want something that returns MyApplication.ViewModels.MyViewModel type. If I'm using concreate class as proxies, BaseType returns the actual class that is being proxied, but when using the interface, BaseType would return System.Object.

Answer

Hadi Eskandari picture Hadi Eskandari · Sep 14, 2009

It seems you can do the following to get the actual type:

(proxy As IProxyTargetAccessor).DynProxyGetTarget().GetType()