1

I have a valid for a XmlSerializer type:

public class MyItem
{
  public MyItem() {
    Values = new List<string>();
  }

  public List<string> Values { get; private set; }
}

It successfully serialized and deserialized (Full code example here http://ideone.com/s2PvSB). But, I can not generate .XmlSerializers.dll for this type, SGen failed with:

Microsoft (R) Xml Serialization support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.33440]
Copyright (C) Microsoft Corporation. All rights reserved.
EXEC : error : Unable to generate a temporary class (result=1).
EXEC : error CS0200: Property or indexer 'TestSGen.MyItem.Values' cannot be assigned to -- it is read only

SGen called from a PostBuild event as

"$(TargetFrameworkSDKToolsDirectory)SGen.exe" /Assembly:"$(TargetFileName)" /Type:"TestSGen.MyItem" /Force

Is it a bug in SGen or [somewhere] documented behavior?

Viacheslav Ivanov
  • 1,535
  • 13
  • 22
  • possible duplicate of [Serializing private member data](http://stackoverflow.com/questions/802711/serializing-private-member-data) – Hans Passant Nov 12 '13 at 19:15
  • @HansPassant It's not a duplicate. As you can see (see link http://ideone.com/s2PvSB from my post), my code works correct, serialisation and deserialisation of my type work fine. I have a private setter on a property with collection type. It's significant for a XmlSerializer. My question is "why SGen failed", not "why my type can not be serialized" - because it can. – Viacheslav Ivanov Nov 12 '13 at 21:14
  • 1
    Hmm, it is covered in [this KB-article](http://support.microsoft.com/kb/956847). It is supposed to fail again. – Hans Passant Nov 12 '13 at 21:20
  • Thanks! Looks like SGen broken again :o( – Viacheslav Ivanov Nov 12 '13 at 21:25

1 Answers1

0

This seems strange, as the code linked does indeed work for me. It doesn't work if you change the List<string> to string[]. Is VS doing something strange with collection initializers? Maybe that allows your code to run normally, but SGen doesn't have access to the same trick?

Rob
  • 4,327
  • 6
  • 29
  • 55