C# 属性未被序列化

C# 属性未被序列化,c#,xml-serialization,xmlserializer,reflection.emit,C#,Xml Serialization,Xmlserializer,Reflection.emit,我使用reflection.emit生成一个运行时类,该类的生成似乎很好,如ILSpy中所示: using indice.Edi.Serialization; using IQoneEDIParser.Formats; using System; using System.Xml.Serialization; [XmlRoot("CONTRL"), XmlType("CONTRL")] [Serializable] public class CONTRL : FormatBase {

我使用reflection.emit生成一个运行时类,该类的生成似乎很好,如ILSpy中所示:

using indice.Edi.Serialization;
using IQoneEDIParser.Formats;
using System;
using System.Xml.Serialization;

[XmlRoot("CONTRL"), XmlType("CONTRL")]
[Serializable]
public class CONTRL : FormatBase
{
    private string _syntaxkennung;

    private int _sintaxversion;

    private string _absenderid;

    private string _absendercodeunb;

    private string _empfängerid;

    private string _empfängercodeunb;

    private string _dokumentdatum;

    private string _dokumentzeit;

    private string _datenaustauschreferenz;

[EdiValue("X(4)", "UNB/0"), XmlElement("Syntaxkennung", typeof(string))]
public string Syntaxkennung
{
    get
    {
        return this._syntaxkennung;
    }
    set
    {
        this._syntaxkennung = value;
    }
}

[EdiValue("9(1)", "UNB/0/1"), XmlElement("Sintaxversion", typeof(int))]
public int Sintaxversion
{
    get
    {
        return this._sintaxversion;
    }
    set
    {
        this._sintaxversion = value;
    }
}

[EdiValue("X(35)", "UNB/1/0"), XmlElement("AbsenderID", typeof(string))]
public string AbsenderID
{
    get
    {
        return this._absenderid;
    }
    set
    {
        this._absenderid = value;
    }
}

[EdiValue("X(4)", "UNB/1/1"), XmlElement("AbsenderCodeUNB", typeof(string))]
public string AbsenderCodeUNB
{
    get
    {
        return this._absendercodeunb;
    }
    set
    {
        this._absendercodeunb = value;
    }
}

[EdiValue("X(35)", "UNB/2/0"), XmlElement("EmpfängerID", typeof(string))]
public string EmpfängerID
{
    get
    {
        return this._empfängerid;
    }
    set
    {
        this._empfängerid = value;
    }
}

[EdiValue("X(4)", "UNB/2/1"), XmlElement("EmpfängerCodeUNB", typeof(string))]
public string EmpfängerCodeUNB
{
    get
    {
        return this._empfängercodeunb;
    }
    set
    {
        this._empfängercodeunb = value;
    }
}

[EdiValue("X(6)", "UNB/3/0"), XmlElement("Dokumentdatum", typeof(string))]
public string Dokumentdatum
{
    get
    {
        return this._dokumentdatum;
    }
    set
    {
        this._dokumentdatum = value;
    }
}

[EdiValue("X(4)", "UNB/3/1"), XmlElement("Dokumentzeit", typeof(string))]
public string Dokumentzeit
{
    get
    {
        return this._dokumentzeit;
    }
    set
    {
        this._dokumentzeit = value;
    }
}

[EdiValue("X(14)", "UNB/4/0"), XmlElement("Datenaustauschreferenz", typeof(string))]
public string Datenaustauschreferenz
{
    get
    {
        return this._datenaustauschreferenz;
    }
    set
    {
        this._datenaustauschreferenz = value;
    }
}
}
出于任何未知的原因,唯一序列化为Xml的属性是Syntaxversion(唯一一个是Integer类型)

下面是序列化方法:

public static String SerializeToXml(FormatBase toSerialize, Type inType)
    {

        XmlWriterSettings xmlSettings = new XmlWriterSettings
        {
            Encoding = Encoding.UTF8,
            Indent = true,
            OmitXmlDeclaration = true,
            NewLineOnAttributes = true,
            CloseOutput = true
        };

        using (StringWriter stringWriter = new StringWriter())
        {
            XmlSerializer serializer2 = new XmlSerializer(inType);
            using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, xmlSettings))
            {
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", "");

                serializer2.Serialize(xmlWriter, toSerialize, ns);

                return stringWriter.ToString();
            }
        }
    }
你知道为什么其他属性没有被序列化吗?提前谢谢

编辑_1: 在进一步的测试之后,我复制粘贴生成的类并在我的项目中将其创建为“普通”类型,再次尝试序列化方法,并按预期工作(请参见EDIT_2)

它是否可能是反射.Emit问题和/或我的运行时程序集

编辑2: Xml序列化的预期结果:

<CONTRL>
  <Syntaxkennung>UNOC</Syntaxkennung>
  <Sintaxversion>3</Sintaxversion>
  <AbsenderID>9904325000003</AbsenderID>
  <AbsenderCodeUNB>500</AbsenderCodeUNB>
  <EmpfängerID>9900080000007</EmpfängerID>
  <EmpfängerCodeUNB>500</EmpfängerCodeUNB>
  <Dokumentdatum>161007</Dokumentdatum>
  <Dokumentzeit>1723</Dokumentzeit>
</CONTRL>

优尼科
3.
9904325000003
500
9900080000007
500
161007
1723
收到:

<CONTRL>
  <Sintaxversion>3</Sintaxversion>
</CONTRL>

3.

我最终在FormatBase(基类)中实现了IXmlSerializable


并按预期工作。

我在代码中的任何地方都找不到
Syntaxversion
,没有输入xml,没有预期结果,而是收到。寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:。我将修改有关该问题的问题。Sintaxversion是生成的类中的属性之一。遗憾的是,我不能提供任何测试用例,因为生成类的代码非常长。私有属性不会序列化。使它们成为public.Ofc,但如果您查看该类,您看到的私有内容是public属性的支持字段。
public void WriteXml(XmlWriter writer)
    {
        foreach (PropertyInfo property in GetType().GetProperties())
        {
            writer.WriteElementString(property.Name, property.GetValue(this)?.ToString());
        }
    }