DisplayAttribute name property not working in Silverlight

后端 未结 1 570
刺人心
刺人心 2021-01-18 17:05

I am binding DataGrid.ItemsSource property to the List object. I am getting datas through Silverlight-enabled WCF

相关标签:
1条回答
  • 2021-01-18 17:56

    The problem is the WCF DataContract is an inter-operable mechanism that can be used across languages and platforms.

    If you take a look to serialized data generated by the DataContractSerializer (or its code in System.Runtime.Serialization.dll, specifically InternalWriteObjectXyz() methods) you'll see that it merely serializes values into a simple XML message. Nothing related to .NET Framework will be there so all kind of attributes, both custom and compiler generated, will be stripped out and won't even received by the client.

    It works creating a copy of your data and sending them from server to client, clients will then create a new class with the same signature. Note: a NEW CLASS with the same signature, NOT JUST A NEW OBJECT of the original class.

    Of course there are some workaround for this. You may write your own serializer (see this post on SO for an example) or your own ISerializationSurrogate.

    If you can deploy/share your assemblies to your clients you have a nice workaround: just deploy them and DataContractSerializer will build the right object on your clients (exactly the same one you had on the server, with all its attributes). Just remember that:

    • If custom attributes comes from run-time values (for example because of localization) then they'll be resolved on the client, not on the server (because attributes will be created on the client, their values won't be included in the XML message).
    • In the client application you need to add a reference to the assembly that contains your types.
    • When you add your service reference you have to instruct VS to use them (or it'll create proxies), in the Service Reference Settings dialog select Reuse types in referenced assemblies (you can limit this to only assemblies you want to share).
    0 讨论(0)
提交回复
热议问题