Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Problems with read only properties
4 replies. Latest Post by Jac_P on July 7, 2009.
(0)
mk20
Member
0 points
2 Posts
07-05-2009 6:15 PM |
I'm using a web service to populate a datagrid within a Silverlight 2 application. The web service returns an array of objects derived from a class alled clsCompany. The class has a prpoerty called ID which is read only, however while the property is accessible within the objects before the web service returns them the property is unavailable at the client end.
If I alter the class and make the property read/write (by adding a set section to the property declaration) and update the reference to the service then the property appears. I've also confirmed this by adding a few extra properties just for testing. It seems strange that the silverlight client doesn't seem to see any read only properties within the objects it is receiving.
You may just think "well why not just make the property read/write) but while this is possible for the current property in question there will probably be instances while developing my app where this will not be possible.
mrjvdveen
Participant
1937 points
366 Posts
07-06-2009 3:35 AM |
This is caused by the fact that the serializer, that transforms the object instance into XML and back needs to access the property in order to set it as it is recieved by either the service or the client. You could make it working with some attributes, giving the serializer access to protected code in a class.
Put these above your class declaration and it should be fine.
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)] [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.SerializationFormatter)]
HTH.
07-06-2009 7:40 AM |
Thanks for the suggestion but I put those in and it's made no difference.
For now I'll just have to add empty set sections to my read only property declarations so that I can move on with the project while I figure out a soution.
07-07-2009 6:15 AM |
That shouldn't work either, because the serializer can not set the value of the property on deserializing the object. Did you add a protected set as well as add the attributes on the business class?
Jac_P
1044 points
189 Posts
07-07-2009 6:26 AM |
Readonly properties are not serialized \ deserialized in silverlight.
You will have to have public(default) get and set for every property that you want on the client side.
If you were using a WCF service, you will be getting some compile/runtime exceptions on putting the <DataMember> attribute over a readonly or writeonly poperty.
Thanks
Jac