If you have ever used sgen.exe to generate/precompile serialization-assemblies, I’m sure you are surprised by the fact that it doesn’t allow you to specify multiple types to generate the classes for. After trying to find an alternative (and failed to do so), I decided to see if it’s possible to disassemble and alter sgen’s sourcecode. It is surprisingly simple. In this post, I’ll guide you through the steps I have taken.
First, download and install Reflector. Then locate sgen.exe. On my laptop, with VS2010 installed, it’s located at C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin. Open sgen.exe in Reflector, right-click sgen and choose ‘Export’. This will disassemble the executable and export all it’s content including a .csproj file; very convenient.
We now need to alter sgen’s source at a couple locations.
1. there is a fixed, absolute path to the key file originally used to sign the assembly. From the AssemblyInfo.cs remove:
If desired, sign the assembly with your own key file.
2. in the file CommonResStrings.cs the class Assembly is used, but the corresponding using statement is missing. Add the using statement:
3. next up is SgenTool\Sgen.cs. In the method ‘Run’ change:
Then change the signature of the GenerateAssembly method into:
That’s it! Compile and enjoy your new multi-type-capable sgen.exe! You simply need to provide multiple /t command-line arguments.
note: I’ve written this post out-of-the-office and it’s a little untested. But I’m pretty sure, these are the steps I’ve did. If something’s not working for you, let me know and I’ll see what I can do. Also I’m not very thrilled about the current code-formatting in this post; I might fix that someday