NHibernate代理的JSON.Net序列化(NH 3.3.2.4000) [英] JSON.Net Serialization of NHibernate Proxies (NH 3.3.2.4000)

查看:97
本文介绍了NHibernate代理的JSON.Net序列化(NH 3.3.2.4000)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍然在让Json.Net和NHibernate很好地玩在一起方面一直遇到困难.即,在让Json.NET序列化代理的NHibernate对象中.

I'm still having continuous difficulties getting Json.Net and NHibernate to play nicely together. Namely, in getting Json.NET to serialize a proxied NHibernate object.

我已遵循建议此处接受答案和修正,但不要掷骰子.

I've followed the recommendations here, both for the accepted answer and the ammendments, but no dice.

上述解决方案的最大问题是,现代版本的NHibernate似乎正在使用INHibernateProxyProxy接口来创建代理(而不是INHibernateProxy?还有人可以确认吗?),在本例中,其基类为NHibernate.Proxy.DynamicProxy.ProxyDummy,当我尝试使用自定义的scontract解析器创建Json合同时,该信息不显示任何有关基础对象的信息,即:

The biggest problem with the above solution is that it seems that modern versions of NHibernate are using the INHibernateProxyProxy interface to create proxies (rather than INHibernateProxy? Can anyne else confirm this?), whose base class in my case is NHibernate.Proxy.DynamicProxy.ProxyDummy, which reveals nothing about the underlying object when I attempt to create the Json constract using my custom scontract resolver, ie:

    protected override JsonContract CreateContract(Type objectType)
    {
        if (typeof(NHibernate.Proxy.INHibernateProxy).IsAssignableFrom(objectType))
            return base.CreateContract(objectType.BaseType);
        else
            return base.CreateContract(objectType);
    }

有人对如何有效处理INHibernateProxyProxy有任何建议吗?

Does anyone have any advice for how to deal with INHibernateProxyProxy effectively?

推荐答案

找到了它.原始类型可通过.GetInterfaces()获得,即:

Found it. The original type is available via .GetInterfaces(), ie:

    protected override JsonContract CreateContract(Type objectType)
    {
        if (typeof (INHibernateProxy).IsAssignableFrom(objectType))
        {
            var oType = objectType.GetInterfaces().FirstOrDefault(i => i.FullName.StartsWith("Your.Domain.Namespace"));
            return oType != null ? base.CreateContract(oType) : base.CreateContract(objectType.BaseType);
        }
        return base.CreateContract(objectType);
    }

这篇关于NHibernate代理的JSON.Net序列化(NH 3.3.2.4000)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆