Note that there are some explanatory texts on larger screens.

plurals
  1. POWCF DataContract serialization of read-only properties?
    text
    copied!<p>Whenever I use WCF, I always try to make immutable classes that end up going over the wire (i.e. parameters set in constructor, properties are read-only). However, this gets in the way of WCF serialization, which demands that all properties be Public get/set (which makes sense, because it has to deserialize them)</p> <p>Even in <a href="https://stackoverflow.com/questions/508661/wcf-datacontract-set-get-not-getting-executed">this related post</a>, I see that their solution ended up making everything Public, which violates my sense of good programming. Is there any way around this? Do I have to just settle for this solution or something like popsicle immutability and be happy with it?</p> <p>The other thing I tried was something like this, where I'd have a base class for everything and a derived class that made the set useless:</p> <pre><code>/// &lt;summary&gt; /// This represents a discovered virtual-machine template that can be /// instantiated into a RunningVirtualMachine /// &lt;/summary&gt; [DataContract] [XmlRoot("VMTemplate")] public class VirtualMachineTemplateBase { [DataMember] public virtual ulong SizeInBytes { get; set; } } /// &lt;summary&gt; /// This class is the real guts of VirtualMachineTemplate that we're hiding /// from the base class. /// &lt;/summary&gt; [XmlInclude(typeof(VirtualMachineTemplateBase))] public class VirtualMachineTemplate : VirtualMachineTemplateBase, IXmlPicklable, IEnableLogger { ulong _SizeInBytes; public override ulong SizeInBytes { get { return _SizeInBytes; } set { } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload