Serialize read-only properties

First up, apologies for the long post, i just like to be as descriptive as possible in my opening post.


I have written a small webservice that (attempts to) return a class.

Basic Class Layout:


<Serializable()> _
PublicClass User

Private _NTloginAsString
Private _DisplayNameAsString
Private _DepartmentAsString
Private _EmailAddressAsString

PublicReadOnlyProperty NTlogin()AsString
Get
Return _NTlogin
EndGet
EndProperty

PublicReadOnlyProperty DisplayName()AsString
Get
Return _DisplayName
EndGet
EndProperty

PublicReadOnlyProperty Department()AsString
Get
Return _Department
EndGet
EndProperty

PublicReadOnlyProperty EmailAddress()AsString
Get
Return _EmailAddress
EndGet
EndProperty


PublicSubNew()
MyBase.New()
EndSub


PublicSubNew(ByVal aAsString,ByVal bAsString,ByVal cAsString,ByVal dAsString)
_NTlogin = a
_DisplayName = b
_Department = c
_EmailAddress = d
EndSub


EndClass


This class is populated and returned by a web method:



<WebMethod()> _
PublicFunction get_Specific_User_coll(ByVal ntLoginAsString)As ButtyBox.User


_Conn =New SqlConnection(_ConnS)
_Comm =New SqlCommand("get_User", _Conn)
_Comm.CommandType = Data.CommandType.StoredProcedure


_Para =New SqlParameter("@NT_Login", Data.SqlDbType.VarChar)
_Para.Value = ntLogin
_Comm.Parameters.Add(_Para)


Dim dsAsNew Data.DataSet("User")
Dim daAsNew SqlDataAdapter(_Comm)
da.Fill(ds)


_Para =Nothing
_Comm =Nothing
_Conn =Nothing
da =Nothing


Dim rowAs Data.DataRow
Dim myUserOAsNew ButtyBox.User
ForEach rowIn ds.Tables(0).Rows
Dim myUserAsNew ButtyBox.User(row.Item(0).ToString, row.Item(1).ToString, row.Item(2).ToString, row.Item(3).ToString)
myUserO = myUser
Next

row =Nothing
ds.Clear()
ds =Nothing
Return myUserO


EndFunction



This method is an interim test method, as the final output will be a collection of these classes.


If i run the above, it all runs fine, but im always left with empty results, e.g.

<?xml version="1.0" encoding="utf-8" ?>
<User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://blahblah.com/" />

If i set the properties to read-write, or export the data using a dataset, then the results are displayed, e.g.

I change the readonly property of NTlogin to a read-write property:


PublicProperty NTlogin()AsString
Get
Return _NTlogin
EndGet
Set(ByVal valueAsString)
_NTlogin = value
EndSet
EndProperty


then the results are returned as:

<?xml version="1.0" encoding="utf-8" ?>
- <User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://blahblah.com/">
<ntLogin>ASR\MiWard</ntLogin>
</User>

So, long story short, i can serialize read-write properties, but i really want these to be read-only to force other developers to use my methods for populating the classes. It appears to be a serialization thing where it cannot reverse lookup the values as it cant get into the read-only properties.

i have read some articles on the iSerializabe interface, but have made no head way with it at all (ie, i get the same results)


Anyone here know how to serialize read-only properties?
is it even possible or am i just missing some glaringly obvious fact here?


Thanks in advance,
Mike.

[8914 byte] By [MaaWaa] at [2008-2-13]
# 1
Well, unfortunately the short answer to your long post is that XML Serialization has no support for non-public or read-only properties. That being said, there's nothing that says your setter has to actualy do anything; it just has to exist. You could take the passive-aggresive route and just have an empty setter, then make an obscure comment in your documentation about the fact that the properties are really read-only in nature (and blame the whole thing on Microsoft of course). Alternatively, you could throw an exception in your setter, which will give developers consuming your class better debugging info.

Cheers,
Todd Gray

Todd.Net at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 2
Why Microsoft doesnt provide support for XML serialization to private and read only members?
Thanks,
Suresh.
sureshsundar007 at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 3
Default xml serialize uses uses reflection, you can't even have a type without defautl constructor
You can try to do your own IXmlSerialize, or doing the Xml doc yourself. the most flexible, you can have the cake and eat it.
erymuzuan at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 4

Suresh -- we hear you, and it's coming with the new [DataContract] serialization system in Windows Communication Foundation (a.k.a. Indigo).

The new serialization removes many of the limitations of the XmlSerializer. You'll be able to serialize properties or fields, and those properties can be public, private, or even read-only. Also, the requirement to implement a default parameterless constructor will be removed.

If you'd like to get a first look at this new technology, you can take a look at Beta 1 of WinFX here.

Hope that helps
-steve

Program Manager, Windows Communication Foundation

SteveMaine at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...

.NET Development

Site Classified