本文介绍了集合和枚举器需要如何修复无参数构造函数。错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个从xml文件中读取值的程序。 i有xml元素需要像这样读取: < 设备 > < item > 114301725 < / item > < item > 111301657 < / item > < item > 113301688 < / item > < item > 100201432 < / item > < item > 112301596 < / item > < item > 100201432 < / item > < item > 110301719 < / item > < / equipment > 但是当我尝试运行程序时它会显示错误: ---> System.InvalidOperationException:无法反序列化global :: WorldServer.Data.Templates.Npc2.NpcEquippedGear。集合和枚举器需要无参数构造函数。 那我该怎么做才能解决这个错误? 我的尝试: 这是我的代码阅读 [XmlElement( equipment)] public NpcEquippedGear设备; 和NpcEquipedGear类: public class NpcEquippedGear:IEnumerable< KeyValuePair< EquipmentSlot,ItemTemplate>> { 私人字典< EquipmentSlot,ItemTemplate>项目; private int mask; private NpcEquipmentList v; public NpcEquippedGear(NpcEquipmentList v) { this .v = v; } public void 添加( object o) { } public int getItemsMask() { if (items == null ) init(); 返回掩码; } public void init() { lock (此) { if (items == null ) { items = new Dictionary< EquipmentSlot,ItemTemplate>(); foreach (ItemTemplate item in v.items) { if (items [item.EquipmentSlot] == null ) { items .Add(item.EquipmentSlot,item); mask | =( int )item.EquipmentSlot; } } } v = null ; } } public ItemTemplate GetItem(EquipmentSlot slot) { return items!= null ? items [slot]: null ; } public IEnumerator GetEnumerator() { return GetEnumerator(); } IEnumerator< KeyValuePair< EquipmentSlot,ItemTemplate>> IEnumerable< KeyValuePair< EquipmentSlot,ItemTemplate>> .GetEnumerator() { return items.GetEnumerator(); } } 解决方案 添加不接受参数的构造函数的重载。错误告诉你究竟出了什么问题。 public NpcEquippedGear() {} 当然,你可以让自己更容易,并从 IEnumerable 到列表: public class NpcEquippedGear:List< KeyValuePair< EquipmentSlot,ItemTemplate>> { ... } 甚至更容易,继承自 Dictionary ,它是 KeyValuePair 对象的集合: public class NpcEquippedGear:Dictionary< EquipmentSlot,ItemTemplate> { ... } 真的没有必要让事情变得比必要的。 错误信息对此非常清楚。你需要添加一个不带任何参数的公共构造函数。 反序列化代码创建你的集合的一个实例,但永远不会知道要传递什么因此它需要一种方法来创建集合的实例而不传递任何参数。 i have a program to read values from xml file.i have xml element need to be read like this: <equipment> <item>114301725</item> <item>111301657</item> <item>113301688</item> <item>100201432</item> <item>112301596</item> <item>100201432</item> <item>110301719</item></equipment>but when i try to run program its show me error:---> System.InvalidOperationException: Could not deserialize global::WorldServer.Data.Templates.Npc2.NpcEquippedGear. Parameterless constructor is required for collections and enumerators.So how can i do to fix this error?What I have tried:Here is my code to read[XmlElement("equipment")] public NpcEquippedGear equipment;and NpcEquipedGear class:public class NpcEquippedGear : IEnumerable<KeyValuePair<EquipmentSlot, ItemTemplate>> { private Dictionary<EquipmentSlot, ItemTemplate> items; private int mask; private NpcEquipmentList v; public NpcEquippedGear(NpcEquipmentList v) { this.v = v; } public void Add(object o) { } public int getItemsMask() { if (items == null) init(); return mask; } public void init() { lock(this) { if (items == null) { items = new Dictionary<EquipmentSlot, ItemTemplate>(); foreach (ItemTemplate item in v.items) { if (items[item.EquipmentSlot] == null) { items.Add(item.EquipmentSlot, item); mask |= (int)item.EquipmentSlot; } } } v = null; } } public ItemTemplate GetItem(EquipmentSlot slot) { return items != null ? items[slot] : null; } public IEnumerator GetEnumerator() { return GetEnumerator(); } IEnumerator<KeyValuePair<EquipmentSlot, ItemTemplate>> IEnumerable<KeyValuePair<EquipmentSlot, ItemTemplate>>.GetEnumerator() { return items.GetEnumerator(); } } 解决方案 Add an overload of the constructor that doesn't accept parameters. The error tells you exactly what's wrong.public NpcEquippedGear(){}Of course, you could make things easier on yourself and change the inherited class from IEnumerable to List:public class NpcEquippedGear : List<KeyValuePair<EquipmentSlot, ItemTemplate>>{...}or even easier, inherit from Dictionary, which is ALREADY a collection of KeyValuePair objects:public class NpcEquippedGear : Dictionary<EquipmentSlot, ItemTemplate>{...}There really is no need to make things harder than necessary.The error message is really clear about this. You need to add a public constructor that doesn't take any parameters.The deserialization code creates an instance of your collection but will never have a clue about what to pass to it so it needs a way to create an instance of your collection without passing any parameters. 这篇关于集合和枚举器需要如何修复无参数构造函数。错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-31 19:55