无法序列化列表<>使用 DataContractJsonSerializer DynamicProxy2 生成的对象

发布于 2024-09-16 22:55:30 字数 1709 浏览 12 评论 0 原文

我在使用 System.Runtime.Serialization.Json.DataContractJsonSerializer 序列化代理对象的 List 时遇到问题。它对于单个代理对象工作得很好,但是列表使它崩溃。像这样的事情:

using System.Collections.Generic;
using System.Runtime.Serialization;
using Castle.DynamicProxy;
using System.IO;
using NUnit.Framework;

[DataContract] 
public class SimpleViewModel 
{ 
    [DataMember] 
    public virtual int ID { get; set; } 
} 
[Test] 
public void TestSerializeArray() 
{ 
    // Generates a proxy of type "SimpleViewModelProxy"
    var proxyModel = (new ProxyGenerator()).CreateClassProxy<SimpleViewModel>(); 
    proxyModel.ID = 1; 
    //Put it into List<> (it can handle a single item without issue!) 
    var list = new List<SimpleViewModel> { proxyModel }; 
    var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<SimpleViewModel>)); 
    using (var stringWriter = new MemoryStream()) 
    { 
        serializer.WriteObject(stringWriter, list); //BOOM CRASH! 
    } 
} 

这样做会给我带来以下异常:

System.Runtime.Serialization.SerializationException : 类型 'Castle.Proxies.SimpleViewModelProxy' 带有数据合约名称 'SimpleViewModelProxy:http://schemas.datacontract.org/2004/07/ Castle.Proxies' 不是预期的。 考虑使用 DataContractResolver 或添加任何静态未知的类型 到已知类型列表 - 对于 例如,通过使用 KnownTypeAttribute 属性或通过 将它们添加到已知列表中 类型传递给 DataContractSerializer。

我可以序列化单个“SimpleViewModelProxy”对象或 List,但不能序列化 List。有没有人有过让这个工作的经验?他们可以提供一些关于我做错了什么的指示吗?

I'm running into an issue using System.Runtime.Serialization.Json.DataContractJsonSerializer to serialize a List<T> of proxied objects. It works fine with a single proxied object, but the List makes it blow up. Something like this:

using System.Collections.Generic;
using System.Runtime.Serialization;
using Castle.DynamicProxy;
using System.IO;
using NUnit.Framework;

[DataContract] 
public class SimpleViewModel 
{ 
    [DataMember] 
    public virtual int ID { get; set; } 
} 
[Test] 
public void TestSerializeArray() 
{ 
    // Generates a proxy of type "SimpleViewModelProxy"
    var proxyModel = (new ProxyGenerator()).CreateClassProxy<SimpleViewModel>(); 
    proxyModel.ID = 1; 
    //Put it into List<> (it can handle a single item without issue!) 
    var list = new List<SimpleViewModel> { proxyModel }; 
    var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<SimpleViewModel>)); 
    using (var stringWriter = new MemoryStream()) 
    { 
        serializer.WriteObject(stringWriter, list); //BOOM CRASH! 
    } 
} 

Doing this gives me the following exception:

System.Runtime.Serialization.SerializationException
: Type
'Castle.Proxies.SimpleViewModelProxy'
with data contract name
'SimpleViewModelProxy:http://schemas.datacontract.org/2004/07/
Castle.Proxies' is not expected.
Consider using a DataContractResolver
or add any types not known statically
to the list of known types - for
example, by using the
KnownTypeAttribute attribute or by
adding them to the list of known
types passed to
DataContractSerializer.

I'm able to serialize either a single "SimpleViewModelProxy" object, or a List<SimpleViewModel>, but not a List<SimpleViewModelProxy>. Has anyone had any experience getting this to work? Can they provide some pointers on what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰魂雪魄 2024-09-23 22:55:30

您可以尝试将代理的类型添加到已知类型列表中:

var serializer = new DataContractJsonSerializer(
    typeof(List<SimpleViewModel>),
    new[] { proxyModel.GetType() });

You can try to add the type of the proxy to the list of known types:

var serializer = new DataContractJsonSerializer(
    typeof(List<SimpleViewModel>),
    new[] { proxyModel.GetType() });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文