Wednesday, July 22, 2009

How to get type masked by Castle Dynamic Proxy

Hello there, back from my vacation.

When writing "ORM" for Sharepoint lists I've encountered a problem with Castle Dynamic Proxy. I had to get the type which was wrapped with proxy and had no way to get that type in any other way besides getting it from the proxy itself.
Here's a little hack that does that through reflection:
var objProxy = obj as IProxyTargetAccessor;
if (objProxy != null)
{
var proxyTargetType= (Type)objProxy.GetType().GetField("typeTokenCache").GetValue(objProxy);
}
Note, however, that implementation of Dynamic Proxy might change and this hack will stop working then.

P.S. If anybody knows how to do it in a "right" way, post please.