无效操作异常,C# XML 错误
Posted
技术标签:
【中文标题】无效操作异常,C# XML 错误【英文标题】:Invalid operation exception , C# XML Error 【发布时间】:2017-06-21 04:27:39 【问题描述】:嘿嘿,我正在为统一游戏制作一个插件(以下错误),所以我可能正在为我的主要插件制作这个插件并阅读排名。我不知道是什么错误,也没有看到任何关于此的帖子。这是我的错误:
[XmlArrayItem(ElementName = "Rank")]
public List<Rank> Ranks;
public void LoadDefaults()
Ranks = new List<Rank>()
new Rank() RankNumber = 0, GroupID = "Default" ,Messages = new List<Message>() new Message("yo") , new Message("yo")
;
public class Rank
public Rank()
public int RankNumber;
public string GroupID;
public List<Message> Messages;
public class Message
public Message(string Rankmessage) Rankmessage = RankMessage;
[XmlAttribute("RankMessage")]
public string RankMessage;
错误:
[2/3/2017 7:47:23 PM] [Error] InvalidOperationException: RanksUnlocker.RanksUnlockerConfig+Message cannot be serialized because it does not have a default public constructor - System.Xml.Serialization.ReflectionHelper.CheckSerializableType (System.Type type, Boolean allowPrivateConstructors)
System.Xml.Serialization.XmlReflectionImporter.ImportClassMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
Rethrow as InvalidOperationException: There was an error reflecting type 'RanksUnlocker.RanksUnlockerConfig+Message'.
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Type type, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
System.Xml.Serialization.XmlReflectionImporter.ImportListMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace, System.Xml.Serialization.XmlAttributes atts, Int32 nestingLevel)
System.Xml.Serialization.XmlReflectionImporter.ImportListMapping (System.Type type, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace, System.Xml.Serialization.XmlAttributes atts, Int32 nestingLevel)
System.Xml.Serialization.XmlReflectionImporter.CreateMapMember (System.Type declaringType, System.Xml.Serialization.XmlReflectionMember rmember, System.String defaultNamespace)
System.Xml.Serialization.XmlReflectionImporter.ImportClassMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
Rethrow as InvalidOperationException: There was an error reflecting field 'Messages'.
System.Xml.Serialization.XmlReflectionImporter.ImportClassMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
Rethrow as InvalidOperationException: There was an error reflecting type 'RanksUnlocker.RanksUnlockerConfig+Rank'.
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Type type, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
System.Xml.Serialization.XmlReflectionImporter.ImportListMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace, System.Xml.Serialization.XmlAttributes atts, Int32 nestingLevel)
System.Xml.Serialization.XmlReflectionImporter.ImportListMapping (System.Type type, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace, System.Xml.Serialization.XmlAttributes atts, Int32 nestingLevel)
System.Xml.Serialization.XmlReflectionImporter.CreateMapMember (System.Type declaringType, System.Xml.Serialization.XmlReflectionMember rmember, System.String defaultNamespace)
System.Xml.Serialization.XmlReflectionImporter.ImportClassMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
Rethrow as InvalidOperationException: There was an error reflecting field 'Ranks'.
System.Xml.Serialization.XmlReflectionImporter.ImportClassMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
Rethrow as InvalidOperationException: There was an error reflecting type 'RanksUnlocker.RanksUnlockerConfig'.
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Type type, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
System.Xml.Serialization.XmlSerializer..ctor (System.Type type, System.Xml.Serialization.XmlAttributeOverrides overrides, System.Type[] extraTypes, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
System.Xml.Serialization.XmlSerializer..ctor (System.Type type, System.Type[] extraTypes)
Rocket.Core.Assets.XMLFileAsset`1[RanksUnlocker.RanksUnlockerConfig]..ctor (System.String file, System.Type[] extraTypes, RanksUnlocker.RanksUnlockerConfig defaultInstance)
Rocket.Core.Plugins.RocketPlugin`1[RocketPluginConfiguration]..ctor ()
RanksUnlocker.RanksUnlocker..ctor ()
UnityEngine.GameObject:.ctor(String, Type[])
Rocket.Core.Plugins.RocketPluginManager:loadPlugins()
Rocket.Core.Plugins.RocketPluginManager:Start()
【问题讨论】:
错误是不言自明的。您的嵌套类型RanksUnlocker.RanksUnlockerConfig+Message
必须有一个公共 无参数 构造函数。参见例如here。
【参考方案1】:
您需要向Message
类添加一个构造函数。它需要是public
,并且需要接受零参数。
您已经拥有的现有构造函数对此不起作用,因为 XML 序列化过程不知道如何处理带参数的构造函数。您也可以保留现有的构造函数,只需添加一个不带参数的新构造函数。
【讨论】:
谢谢!我添加了 public Message()以上是关于无效操作异常,C# XML 错误的主要内容,如果未能解决你的问题,请参考以下文章
C#解析网站数据出错,显示错误为“0”的值对于“index”无效。“index”应介于 0 和 -1 之间。
尝试从 Visual c# 程序更新 oracle 时出现“无效操作。连接已关闭”错误