views:

188

answers:

2

As discussed in http://stackoverflow.com/questions/431203/does-the-order-of-fields-in-c-matter, the order of serializable properties affects, among other things, XmlSerializer output.

But if fields are in 2 files (using partial classes), does anyone know what in fact controls the resulting order? That is, which file's properties comes first?

(Background: I ask this because I've run into a scenario where one of the 2 files is auto-generated from xsd, and the other is manually edited. The test output is different on developer boxes vs. our scripted build box. Presumably this is a side effect of the several differences in the timing and history of the xsd->C# step in the 2 environments. Various ways to fix, but I'd like to understand the compilation process a little better if possible.)

Thanks, Eric

+2  A: 

Nothing is guaranteed per C# spec.

Mehrdad Afshari
Thanks Mehrdad. I'll extend your answer slightly based on my own observations.1. The order is not guaranteed, as you say.2. With VS 2008, the ordering is at least in part a function of file name sort order. That is, renaming the files can affect the order.-Eric
Eric Hirst
A: 

I've found that using the 'easy' approach to making an object by marking it [Serializable] is usually only good enough for very simple implementations.

I would recommend that you implement the IXmlSerializable interface which is pretty easy to do and gives you all the control you need.

STW
Right, we do that in places where we find we more or less have to.In this case, the benefit of easy code maintenance outweighed any need for absolute control. (When possible, our pattern is to treat .xsd as the source, autogenerate C# via xsd.exe as a pre-build step, and extend the partial classes as needed.)The "fix" that I ultimately chose in this case was to simply rename the file containing our extension to the partial class.
Eric Hirst