Identifying NHibernate proxy classes

You can detect if a class is a NHibernate proxy by casting it to (unsurprisingly) INHibernateProxy.

If you need to get the underlying “real” object, use:

Session.GetSessionImplementation().PersistenceContext.Unproxy(proxiedObject)

You don’t need to test for proxies to call Unproxy; it returns the original parameter if it’s not a proxy.

Edit: I now use a different approach to get the underlying object, mostly to work around lazy loading and inheritance: http://sessionfactory.blogspot.com/2010/08/hacking-lazy-loaded-inheritance.html

Leave a Comment