Identifying NHibernate proxy classes

后端 未结 7 2055
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 06:53

I\'m not an NHibernate user; I write a serialization utility library. A user has logged a feature-request that I should handle NHibernate proxy classes, treating them the sa

7条回答
  •  余生分开走
    2020-11-28 07:27

    I'm guessing that you don't really want to access the actual Nhibernate session. This code might better fit your needs:

    /// 
    /// Returns the real type of the given proxy. If the object is not a proxy, it's normal type is returned.
    /// 
    internal static Type GetRealType(this object proxy)
    {
        if (proxy is INHibernateProxy)
        {
            var lazyInitialiser = ((INHibernateProxy)proxy).HibernateLazyInitializer;
            return lazyInitialiser.PersistentClass;
        }
        else
        {
            return proxy.GetType();
        }
    }
    

    Hope that helps.

提交回复
热议问题