Or you can get the class for "free" using this little dirty trick:

Just add the following to your app/web.config file:

<configuration>
 <system.diagnostics>
   <switches>
     <add value="4" name="XmlSerialization.Compilation">
   </switches>
 </system.diagnostics>
</configuration>

This option tells the XmlSerializer not to delete the temporary files
created to generate the temp assembly. It will leave the .cs file it
was trying to compile, a .cmdline file which has the command line
parameters that are sent to the compiler and a .out file which has the
full command line that was used to compile the .cs file.

All of these files are generated it your %USERPROFILE%\Local
Settings\Temp folder (or %TEMP% if .NET 1.1 pre SP1).

Now, all you need to do is include the code from the generated file in
your app, with some modification required. If you put the
serialization in the same assembly, you can serialize internal
members, or if put as an inner class, you serialize even private
members.

Sébastien

On 9/7/06, John Bergman <[EMAIL PROTECTED]> wrote:
I believe you should be able to do it directly using the XmlSerializer,
you will not be able to deserialize it though.

If you need to deserialize it, you will need to handle-code a serializer
for it.

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of James Berry
Sent: Wednesday, September 06, 2006 9:56 AM
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: [ADVANCED-DOTNET] XmlSerialization of immutable objects

Given an immutable class, I would like to use Xml Serialization to
persist and recover this object.  It looks at first glance like Xml
Serialization would require public setters for "A" and "B", which I am
trying to avoid.  Is there a good technique I can use?  Or do I need to
override the serialization?  (This is quite a complex object graph in my
real world example so I'd like to do it as simply as possible).

public class Test {

  public Test(string a, string b) {
    this.a = a;
    this.b = b;
  }

  string a;
  string b;

  public string A {
    get {
      return a;
    }
  }

  public string B {
    get {
      return b;
    }
  }
}

Best wishes
James

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com

View archives and manage your subscription(s) at
http://discuss.develop.com

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com



--
Sébastien Lorion
Software Architect / Architecte organique
[EMAIL PROTECTED]

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to