How to Serialize read-Only Properties?

A

Armin Braun

Hi all,
I am using a certain class as part of a return of a soap service. The
readonly properties are not serialized in the soap response.

Is there a simple way to force them being serialized?

Thanks

Armin
 
S

Scott Swigart

The short answer is that you can't. If you have:

Public Class MyClass
Public ReadOnly Property Name() As String
Get
Return m_name
End Get
End Property
End Class

Theoritically, this could be serialized into a SOAP message, but then what
happens on the other end? It would have to de-serialize this by creating an
instance of MyClass, and then try to set the Name property with data from
the SOAP message. However, it can't set the Name property on the client
side because it's read only.

You could make a "do nothing" set section for the property, and that would
let it serialize, but data would be lost when it's deserialized on the other
end. You could also make a Set section that throws an exception if it's
called. Then, it would be impossible to externally set the property on the
server side, but it would serialize and deserialize fine (the client side
wouldn't automatically have the Set that throws an exception). However, the
property on the client side would not be read-only.

HTH,
Scott Swigart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top