c# - Access XmlAttributesOverrides added attributes in IXmlSerializable methods -


how access xmlattributes applied fields in ixmlserializable object using xmlattributesoverrides?

sample ixmlserializable object:

    public class person : somebaseclass, ixmlserializable {     public string name1;      public string name2;      [xmlignore]     public string name3;      public person()     {     }      public person(string first, string second, string third)     {         name1 = first;         name2 = second;         name3 = third;     }      public xmlschema getschema()     {         return null;     }      public void readxml(xmlreader reader)     {         // ....     }      public void writexml(xmlwriter writer)     {         fieldinfo[] finfo = this.gettype().getfields();          foreach (fieldinfo finf in finfo)         {             fieldattributes attr = finf.attributes;             object[] atts = finf.getcustomattributes(true);              if (atts.length == 0)             {                 // handle field no attributes ... should name1                 // name2 since xmlattributoverrides' xmlignore not                 // included getcustomattributes results.                 writer.writeelementstring(finf.name, (string)finf.getvalue(this));             }             else             {                 // handle field attributes ... should name2 , name3                 // name3 via [xmlignore] compiler generated attribute             }         }     } } 

typical override creation:

        // parent app ...      public xmlserializer createoverrider()     {         xmlattributeoverrides xover = new xmlattributeoverrides();         xmlattributes attrs = new xmlattributes();          attrs.xmlignore = true;         xover.add(typeof(person), "name2", attrs);          xmlserializer xser = new xmlserializer(typeof(person), xover);         return xser;     }      private void button2_click(object sender, eventargs e)     {         // custom xmlserialize         person pson = new person("first", "second", "third");          xmlserializer serializer = createoverrider();         textwriter writer = new streamwriter("personoverride.xml");          serializer.serialize(writer, pson);         writer.close();     }      // etc ... 

created output:

<?xml version="1.0" encoding="utf-8"?><person><name1>first</name1><name2>second</name2></person> 

i need use ixmlserializable overcome inheritance issues 'somebaseclass'.

the problem getcustomarributes doesn't return of attributes added xmlattributeoverrides collection - or i'm doing wrong !?

it's getcustomattributes not supposed return such added attributes, or i'm not supposed use xmlattributeoverrides in ixmlserializable class.

so... ideas or alternatives. taking time.

there no way that.

the reason because xmlserializer generate serializer classes when given objects not ixmlserializable. xml overrides attributes used compile classes differently. xml overrides attributes do not apply @ runtime during serialization or deserialization; why not accessible.

classes inherit ixmlserializable not generate serializer class. if want use xml override attributes going have not override serializer class compiler. use implementation of person instead , let generate serializer class based on given overrides (also run many many times faster):

public class person : somebaseclass {     public string name1;      public string name2;      [xmlignore]     public string name3;      public person()     {     }      public person(string first, string second, string third)     {         name1 = first;         name2 = second;         name3 = third;     } } 

you of course welcome write own serializer class compiler, bit more involved can fit here. implementation should this:

public static type generatepersonserializer(xmlattributeoverrides overrides) {     //here compile class generate type inheriting ixmlserializable     //the serializer logic in class should generated taking     //account given xmlattributeoverrides     //the type returned should type passed new xmlserializer(type type) } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -