![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | read only [DataMember] Hello, Is there a way to create a read only datamember? example [DataContract] public class User { [DataMember] public int Id; //<-- this should be readonly public User(int id) { this.Id = id; } } The client should know about it, but should not be able to change it. thanks Dirk |
| | #2 (permalink) |
| Guest | Re: read only [DataMember] Do it private with only public getter ( without setter ) Arkady "Dirk Reske" <_freak.2k@gmx.net> wrote in message news:uFPbCiXQHHA.3812@TK2MSFTNGP06.phx.gbl... > Hello, > > Is there a way to create a read only datamember? > > example > > [DataContract] > public class User > { > [DataMember] > public int Id; //<-- this should be readonly > > public User(int id) > { > this.Id = id; > } > } > > The client should know about it, but should not be able to change it. > > thanks > Dirk |
| | #3 (permalink) |
| Guest | Re: read only [DataMember] Generally the property is marked as [DataMember], however for this scenario you can mark the underlying field as [DataMember] (and perhaps rename in the attribute to keep you serialization the same). This allows the class to continue to expose the property as read-only, yet allow correct (de)serialization. Marc |
| | #4 (permalink) |
| Guest | Re: read only [DataMember] Not to directly answer your question, but use of a public field (which has unrestricted access since it is just a publically-exposed chunk of memory) instead of a property is a discouraged practice (I believe FxCop will flag it). The ".Net way" to do what you're asking is: private int _Id; public int Id { get { return this._Id; } } |
| |
| |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| READ THIS IF YOU CANNOT GET DRIVE TO READ DISK TO FIX VISTA ERROR! | reap70 | Vista hardware & devices | 1 | 02-10-2008 03:38 PM |
| Folders/files read only/can't create new folder in read only folde | gshillitani | Vista account administration | 10 | 09-25-2007 09:52 AM |
| Read-Host issue, won't store to variable when using multiple read-host lines | bryan.rutkowski@gmail.com | PowerShell | 7 | 02-09-2007 02:40 PM |