本文介绍了"类型预计不会与QUOT;,使用的DataContractSerializer - 但它只是一个简单的类,没有有趣的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重构我的XML序列化,并想我会尝试的DataContractSerializer。一切顺利,直到它需要序列化这个类:

 使用系统;
使用System.Runtime.Serialization;

命名空间VDB_Sync.Model
{
[DataContract(NAME =Konstant)
公共类Konstant:DataFelt
{
    [数据成员]
    私人MySQLDbType的mydataType;
    [数据成员]
    私有对象价值;

    公共Konstant(字符串navn,MySQLDbType的数据类型对象的值)
        :基地(navn,数据类型* Konstant,假的,假的)
    {
        //this.navn = navn;
        this.mydataType =数据类型,
        THIS.VALUE =价值;

        如果(navn.Contains(*Løbenummer))
        {
            navn =* Konstant:+ Convert.ToString(值);
        }
    }

    公共对象值
    {
        得到
        {
            返回值;
        }
    }

}
}
 

它给的我:

*的帮助下,我发现迄今点,集合和类型。我有我的类枚举(MySQLDbType的) - 但得到这样的:我甚至得到了同样的错误,当我没有数据成员宣布在所有:-x所以 - 这是怎么回事吗?我在想什么?

,以供参考,这是我如何序列化它,VDB_SessionController为根:*

 公共无效GemKonfig(VDB_SessionController会话)
    {
        VAR设置=新XmlWriterSettings()
        {
            缩进= TRUE,
            IndentChars =\ t的
        };

        VAR作家= XmlWriter.Create(defaultFile,设置);
        DataContractSerializer的SER =
            新的DataContractSerializer(typeof运算(VDB_SessionController));

        ser.WriteObject(作家,会议);
        writer.Close();
    }
 

解决方案

这是被报道的例外情况是VDB_Sync.Model.Konstant。这意味着,在某处进一步上涨的链条,这个类是被拉到到另一个类和类是一个被序列化。

的问题是,取决于如何Konstant嵌入在这个类(例如,如果它是在一个集合或一个通用的列表),所述的DataContractSerializer未必ppared为反序列化期间其外观$ P $

要解决此问题,您需要将已知类型的属性应用于包含Konstant类。根据您的序列化code,我怀疑这是 VDB_SessionController

所以,尽量修饰这个类的KnownType属性:

  [KnownType(typeof运算(VDB_Sync.Model.Konstant)
公共类VDB_SessionController
 

I'm refactoring my XML-serialization, and figured I'd try the DataContractSerializer.Everything runs smoothly, until it needs to serialize this class:

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;
        }
    }

}
}

It give's me this:

*The help I've found so far points to collections and Types. I do have an enum (MySqlDbType) in my class - but get this: I even get the same error when i have no DataMembers declared at all :-xSo - what's going on here? What am I missing?

for reference, this is how i serialized it, VDB_SessionController being the root:*

    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();
    }
解决方案

The exception that is being reported is for VDB_Sync.Model.Konstant. This means that somewhere further up the chain, this class is being pulled into another class and that class is the one being serialized.

The issue is that depending on how Konstant is embedded in this class (for example, if it is in a collection or a generic list), the DataContractSerializer may not be prepared for its appearance during deserialization.

To resolve this, you need to apply the known-type attribute to the class that contains Konstant. Based on your serialization code, I suspect that this is VDB_SessionController.

So, try decorating this class with the KnownType attribute:

[KnownType(typeof(VDB_Sync.Model.Konstant)]
public class VDB_SessionController

这篇关于"类型预计不会与QUOT;,使用的DataContractSerializer - 但它只是一个简单的类,没有有趣的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 03:53