gpt4 book ai didi

c# - 由 'yield return' 语句支持的 IEnumerable<> 的 DataContract 序列化

转载 作者:行者123 更新时间:2023-11-30 12:33:19 26 4
gpt4 key购买 nike

是否可以序列化一个 IEnumerable 属性,其中的值由“yield return”语句支持?如果可能的话,怎么做?不是,为什么?

每当我尝试这样做时,我都会从 DataContractSerializer 得到一个 NullReferenceException。一个例子:

using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;

namespace IEnumerableTest
{
class Program
{
static void Main(string[] args)
{
DataContractSerializer ser =
new DataContractSerializer(typeof(Test));

using (FileStream writer = new FileStream("test.xml", FileMode.Create))
{
Test test = new Test();
//NullReferenceException thrown by the next call
//if YieldValues is flagged as [DataMember]
ser.WriteObject(writer, test);
}
}
}

[DataContract(Name = "Test")]
public class Test
{
List<int> values = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8 };

//This property serializes without issue
[DataMember]
public IEnumerable<int> Values
{
get
{
return values;
}
}

//Attempting to serialize this member results in a NullReferenceException
[DataMember]
public IEnumerable<int> YieldValues
{
get
{
foreach (int value in values)
{
yield return value;
}
}
}

public Test()
{

}
}
}

异常详情:

System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=System.Runtime.Serialization
StackTrace:
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.OnHandleIsReference(XmlWriterDelegator xmlWriter, DataContract contract, Object obj)
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
at WriteTestToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph)
at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(Stream stream, Object graph)
at IEnumerableTest.Program.Main(String[] args) in F:\Users\Caleb\Documents\Visual Studio 2010\Projects\IEnumerable\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

这是在 .NET 4.0 中,在 Visual Studio 2010 中运行

最佳答案

考虑以下顺序:

var file = File.Open("a.txt");
yield return "";
//#1
yield return new StreamReader(file).ReadToEnd();

想象一下这个序列被枚举到点 #1 并暂停。即使您能够序列化它,您将如何恢复它? DataContractSerializer 无法神奇地重新打开您的文件。

没有安全的方法来恢复/反序列化 yield-backed 序列,因为这样的序列可以做任何。它可以打开一个消息框或格式化您的硬盘。

这就是为什么 C# 语言设计者没有在他们的迭代器类上公开任何可用于序列化或反序列化它们的功能。

仅使用反射手动序列化迭代器类上的字段将始终依赖于编译器实现细节。尚未投入生产。

关于c# - 由 'yield return' 语句支持的 IEnumerable<> 的 DataContract 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9506525/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com