genxs configurationFile [destinationFolder]
configurationFile is configuration file which specifies several information, such as the class for which to generate the reader and writer, the name and namespace of the classes to generate, and a collection of hooks to apply. By using hooks it is possible to customize the behavior of the serializer without needing to modify the generated file, so you can safely regenerate it if the source class is modified.
destinationFolder specifies the folder where the files will be generated.
NOTE: This tool only runs in the Mono runtime, since it uses some internal classes not available in other runtimes.
<configuration> <serializer class="name" assembly="name"> * <reader>name</reader> ? <writer>name</writer> ? <namespace>name</namespace> ? <outFileName>name</outFileName> ? <readerHooks> ? <hook ...> * </readerHooks> <writerHooks> ? <hook ...> * </writerHooks> </serializer> </configuration>
A configuration file can have multiple "serializer" elements, each of which specifies the class for which to generate a serializer together with several generation options. The source class is specified in the following attributes:
Generation options are specified in child elements:
<hook type="name"> <select> ? <typeName>name</typeName> ? <typeAttribute>name</typeAttribute> * <typeMember>name</typeMember> ? </select> <replace>source code</replace> ? <insertBefore>source code</insertBefore> ? <insertAfter>source code</insertAfter> ? </hook>
The "type" attribute specifies the context in which the hook is applied. It can be one of the following:
The "select" element specifies the classes and members to which the hook has to be added. It can contain the following elements:
The hook source code can be specified using any of the following elements:
When writing the code for a hook you can use some special variables that are defined during the code generation process. The variables are the following:
<hook type="type"> <insertAfter> System.Xml.Schema.XmlSchema.Validate$TYPE ($OBJECT); </insertAfter> </hook>
This example specifies the code to be used to deserialize the XmlSchema class:
<hook type="type"> <select> <typeName>System.Xml.Schema.XmlSchema</typeName> </select> <replace> $OBJECT = System.Xml.Schema.XmlSchema.Read (Reader, null); </replace> </hook>
That one specifies the code to be used to read XmlSchema instances:
<hook type="type"> <select> <typeName>System.Xml.Schema.XmlSchema</typeName> </select> <replace>$OBJECT.Write (Writer);</replace> </hook>
With this two hooks the serializer will print some information when serializing the class "MyClass":
<hook type="type"> <select> <typeName>MyNamespace.MyClass</typeName> </select> <insertBefore>Console.WriteLine ("Serializing MyClass");</replace> <insertAfter>Console.WriteLine ("MyClass serialized");</insertAfter> </hook> <hook type="member"> <select> <typeName>MyNamespace.MyClass</typeName> </select> <insertAfter> Console.WriteLine ("Serialized member $MEMBER"); </insertAfter> </hook>
This hook writes an additional element for all types that have the custom attribute "MyAttribute":
<hook type="elements"> <select> <typeAttribute>MyNamespace.MyAttribute</typeAttribute> </select> <insertAfter> Writer.WriteStartElement ("privateData"); Writer.WriteString ($OBJECT.PrivateData); Writer.WriteEndElement (); </insertAfter> </hook>
<configuration> <serializer class="System.Web.Services.Description.ServiceDescription" assembly="System.Web.Services"> <reader>ServiceDescriptionReaderBase</reader> <writer>ServiceDescriptionWriterBase</writer> <namespace>System.Web.Services.Description</namespace> <outFileName>ServiceDescriptionSerializerBase.cs</outFileName> <readerHooks> <hook type="unknownElement"> <select> <typeAttribute>System.Web.Services.Configuration.XmlFormatExtensionPointAttribute</typeAttribute> </select> <replace>ServiceDescription.ReadExtension (Reader, $OBJECT);</replace> </hook> <hook type="type"> <select> <typeName>System.Xml.Schema.XmlSchema</typeName> </select> <replace>$OBJECT = System.Xml.Schema.XmlSchema.Read (Reader, null);</replace> </hook> </readerHooks> <writerHooks> <hook type="elements"> <select> <typeAttribute>System.Web.Services.Configuration.XmlFormatExtensionPointAttribute</typeAttribute> </select> <insertBefore>ServiceDescription.WriteExtensions (Writer, $OBJECT);</insertBefore> </hook> <hook type="type"> <select> <typeName>System.Xml.Schema.XmlSchema</typeName> </select> <replace>$OBJECT.Write (Writer);</replace> </hook> </writerHooks> </serializer> </configuration>