一切运行顺利,直到需要序列化这个类:
using System; using System.Runtime.Serialization; namespace VDB_Sync.Model { [DataContract(Name="Konstant")] public class Konstant : DataFelt { [DataMember] private MySqlDbType mydataType; [DataMember] private object value; public Konstant(string navn,MySqlDbType dataType,object value) : base(navn,dataType,"*Konstant",false,false) { //this.navn = navn; this.mydataType = dataType; this.value = value; if (navn.Contains("*Løbenummer")) { navn = "*Konstant: " + Convert.ToString(value); } } public object Value { get { return value; } } } }
它给我这个:
Type ‘VDB_Sync.Model.Konstant’ with data contract name ‘Konstant:http://schemas.datacontract.org/2004/07/VDB_Sync.Model’ 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.
*我迄今为止找到的帮助指向集合和类型。我有一个枚举(MySqlDbType)在我的类 – 但得到这样:我甚至得到相同的错误时,我没有声明任何DataMembers:-x
那么 – 这里发生了什么?我缺少什么?
为了参考,这是我如何序列化它,VDB_SessionController是根:*
public void GemKonfig(VDB_SessionController session) { var settings = new XmlWriterSettings() { Indent = true,IndentChars = "\t" }; var writer = XmlWriter.Create(defaultFile,settings); DataContractSerializer ser = new DataContractSerializer(typeof(VDB_SessionController)); ser.WriteObject(writer,session); writer.Close(); }
问题是,根据Konstant如何嵌入此类(例如,如果它在集合或通用列表中),DataContractSerializer可能不准备在反序列化期间出现。
要解决此问题,您需要将known-type属性应用于包含Konstant的类。基于你的序列化代码,我怀疑这是VDB_SessionController。
因此,尝试使用KnownType属性装饰此类:
[KnownType(typeof(VDB_Sync.Model.Konstant)] public class VDB_SessionController
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。