2017-03-20 3 views
0

je produis une classe d'exécution à l'aide Reflection.Emit, la génération de la classe semble être bien car il montre à ILSpy:ne se sérialisés

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

Pour une raison inconnue, la seule propriété se sérialisé Xml est le Syntaxversion (le seul qui est de type entier)

Voici la méthode de sérialisation:

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

Toutes les idées sur la raison pour laquelle le reste des propriétés ne sont pas ge té sérialisé? Merci d'avance!

EDIT_1: Après d'autres tests je copier-coller la classe générée et a créé comme un type « normal » dans mon projet, essayez à nouveau la méthode de sérialisation, et le travail comme prévu (voir EDIT_2)

Peut-elle être un problème Reflection.Emit et/ou mon assembly d'exécution?

EDIT_2: Résultat attendu de la sérialisation 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> 

reçu:

<CONTRL> 
    <Sintaxversion>3</Sintaxversion> 
</CONTRL> 
+1

Je ne trouve pas 'Syntaxversion' n'importe où dans votre code, il n'y a pas d'entrée xml, pas de résultat attendu et reçu à la place. Les questions qui cherchent une aide au débogage («pourquoi ce code ne fonctionne-t-il pas?») Doivent inclure le comportement souhaité, un problème ou une erreur spécifique et le code le plus court nécessaire pour le reproduire dans la question elle-même. Les questions sans énoncé de problème clair ne sont pas utiles aux autres lecteurs. Voir: [Comment créer un exemple minimal, complet et vérifiable] (https://stackoverflow.com/help/mcve). – Sinatr

+0

Je vais modifier ma question à ce sujet. – Nekeniehl

+0

Sintaxversion est l'une des propriétés de la classe Generated. Malheureusement je ne peux pas fournir de cas de test car le code pour générer la classe est extrêmement long – Nekeniehl

Répondre

0

Je finis par la mise en œuvre IXmlSerializable dans la FormatBase (classe de base)

public void WriteXml(XmlWriter writer) 
    { 
     foreach (PropertyInfo property in GetType().GetProperties()) 
     { 
      writer.WriteElementString(property.Name, property.GetValue(this)?.ToString()); 
     } 
    } 

Et fonctionne comme prévu.