gpt4 book ai didi

.net - .NET : System. InvalidOperationException : You must implement a default accessor on System. Collections.Generic.Stack`1 中的序列化异常

转载 作者:行者123 更新时间:2023-12-05 08:44:49 27 4
gpt4 key购买 nike

我想做的就是

XmlSerializer serializer = new XmlSerializer(typeof(Stack<int>));

我在运行时得到以下信息:

System.InvalidOperationException :您必须在 System.Collections.Generic.Stack `1 [[System.Int32, mscorlib, 版本=2.0.0.0,Culture=neutral, PublicKeyToken=b77a5c561934e089]] 因为它继承自ICollection。

我不应该序列化Stack<int>吗? ?

最佳答案

由于 Stack 类没有默认访问器(例如通过索引),您无法使用该方法对其进行序列化。

我建议将您的堆栈复制到一个列表,然后序列化该列表。

List<int> serializableLIst = new List<int>( myStack );
XmlSerializer serializer = new XmlSeralizer(typeof(List<int>));

看看这是否效果更好。

关于.net - .NET : System. InvalidOperationException : You must implement a default accessor on System. Collections.Generic.Stack`1 中的序列化异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/398642/

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