[Mono-list] c# serialization

Ricardo Kirkner mono@kirkner.com.ar
Thu, 20 Feb 2003 23:42:01 -0300


This is a multi-part message in MIME format.

------=_NextPart_000_0015_01C2D939.AA731FF0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hello. I am sorry to post this here, because it is a question not =
regarding mono specifically but c# in general, but i did not find =
answers to my question anywhere else.

I would like to serialize a class that has the following structure (this =
is an abstraction)

public var1;
public var2;
public type;

where one instance of this class can only have var1 or var2 =
instantiated. Therefore I introduced variable type. So I can know which =
variable is currently the right one (i.e. type=3Dvar1 or type=3Dvar2).
When it comes to serialization, i would like to get only the "used" =
variable serialized (i.e if type=3Dvar1 then I would like to have onle =
var1 serialized, and not both)

So far i have found that in order to serialize a class i could prepend =
the [Serializable] attribute to my class, and it would serialize every =
public variable. In order not to get variable type serialized, i could =
prepend the [XmlIgnore] attribute and that would do the job; but how can =
i tell that one variable should get serialized based on the contents of =
another variable of an instance of my class. I was thinking of something =
like...

[if this.type=3D"var2" then XmlIgnore]
public var1;

[if this.type=3D"var1" then XmlIgnore]
public var2;

but of course this does not work at all.

If anyone out there knows a hint to give me, I would really apreciate =
it.

With regards, (and apologizing for my english)

Ricardo Kirkner
------=_NextPart_000_0015_01C2D939.AA731FF0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1141" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hello. I am sorry to post this here, =
because it is=20
a question not regarding mono specifically but c# in general, but i did =
not find=20
answers to my question anywhere else.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I would like to serialize a class that =
has the=20
following structure (this is an abstraction)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>public var1;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>public var2;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>public type;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>where one instance of this class can =
only have var1=20
<STRONG>or</STRONG> var2 instantiated. Therefore I introduced variable =
type. So=20
I can know which variable is currently the right one (i.e. type=3Dvar1 =
<STRONG>or=20
</STRONG>type=3Dvar2).</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>When it comes to serialization, i would =
like to get=20
only the "used" variable serialized (i.e if type=3Dvar1 then I would =
like to have=20
onle var1 serialized, and not both)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>So far i have found that in order to =
serialize a=20
class i could prepend the [Serializable]&nbsp;attribute to my class, and =
it=20
would serialize every public variable. In order not to get variable type =

serialized, i could prepend the [XmlIgnore] attribute and that would do =
the job;=20
but how can i tell that one variable should get serialized based on the =
contents=20
of another variable of an instance of my class. I was thinking of =
something=20
like...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>[if this.type=3D"var2" then =
XmlIgnore]</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>public var1;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>[if this.type=3D"var1" then =
XmlIgnore]</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>public var2;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>but of course this does not work at=20
all.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>If anyone out there knows a hint to =
give me, I=20
would really apreciate it.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>With regards, (and apologizing for my=20
english)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Ricardo =
Kirkner</FONT></DIV></BODY></HTML>

------=_NextPart_000_0015_01C2D939.AA731FF0--