How to generate an Xml Serialization assembly for a custom XmlSerializer to prevent csc.exe being launched?


  • I have a custom XmlSerializer within my class to output XML in a different structure to what would be produced with the default serializer. I guess internally .NET still generates a serialization assembly but I would like to generate this assembly after compilation (e.g. using sgen.exe) so it's not generated at runtime. How can I generate a serialization assembly for this custom serializer? If I use sgen for that type it only generates the default serializer.

    In particular the reason I need to generate the serialization assembly is that my .NET assembly is called from within the Internet Explorer process, which runs in Protected Mode. If the .net runtime tries to generate a serialization assembly it calls csc.exe and the user is prompted to allow/deny this process to be run. I don't want users to be prompted! So need all serialization/deserialization to be done without csc.exe.I've tried using sgen.exe /k on the assembly and comparing the .cs file from that with the auto-generated .cs file created when running the program (which I found in c:\users\<myuserame>\appdata\local\temp\low). I can see that the one generated when running the program is the custom XmlSerializer and the one from running sgen.exe is not - for example there are properties with XmlIgnore in the default serializer that don't appear in the sgen.exe version but correctly do appear in the runtime version because the custom XmlSerializer has turned off XmlIgnore on those properties.


    One option I can think of is capturing that .cs that's generated at runtime, putting it in a separate assembly and then including that in my product. Apart from being a bit icky, that raises the problem of how to auto-generate that .cs as part of my build process ...

    Perhaps also worth mentioning: my custom XmlSerializer also serializes nested classes, so maybe there would be auto-generated serializers for them too. My custom serialization is mostly done along the lines below - it adds XmlIgnore to some properties and removes XmlIgnore from others. Many of these properties return objects that would need to be serialized, some of which implement IXmlSerializable, some use default serialization.



    	    public void SerializeToCustomXml1(XmlWriter writer)
    	            CustomXmlSerializer1.Serialize(writer, this);
    	        catch (Exception)
    	    /// <summary>
    	    /// Static serializer so it's created just once. There's another one like this CustomXmlSerializer2 with a slightly different format again.
    	    /// </summary>
    	    private static XmlSerializer CustomXmlSerializer1
    	            if (_customXmlSerializer == null)
                        XmlAttributes dontIgnore = new XmlAttributes();
                        dontIgnore.XmlIgnore = false;
                        XmlAttributes attributes;
    	                XmlAttributeOverrides overrides = new XmlAttributeOverrides();
    					// Include some fields in the XML that wouldn't be there otherwise.
                        overrides.Add(typeof (WebResource), "ID", dontIgnore);
                        overrides.Add(typeof (WebResource), "HasDestinationURLs", dontIgnore);
    	                overrides.Add(typeof (Resource), "AccessDefindBy", dontIgnore);
    	                attributes = new XmlAttributes();
    	                attributes.XmlIgnore = false;
    	                attributes.XmlElements.Add(new XmlElementAttribute("ActionID"));
    	                overrides.Add(typeof(Action), "ID", attributes);
    	                // Instead of serializing the Actions field we serialize CustomActionsXmlSerializer,
    	                // which outputs different content in the XML
    	                overrides.Add(typeof (WebResource), "Actions", ignore);
    	                attributes = new XmlAttributes();
    	                attributes.XmlIgnore = false;
    	                attributes.XmlElements.Add(new XmlElementAttribute("Actions"));
    	                overrides.Add(typeof (WebResource), "CustomActionsXmlSerializer", attributes);					
    					// ... more of these overrides here ... 
    	                _customXmlSerializer1 = new XmlSerializer(typeof(WebResource), overrides);
    	            return _customXmlSerializer1;



    Any ideas?



    (Cross-posted to .NET Base Class Library forum here and Stack Overflow here.)
    Wednesday, September 07, 2011 5:04 PM