开发者

property names are different from original Object in the silverlight

Following is part of service layer which is provided by WCF service :

[Serializable]
public class WaitInfo
{
    private str开发者_StackOverflow中文版ing roomName;
    private string pName;
    private string tagNo;

    public string RoomName 
          { get { return roomName; } set { this.roomName = value; } }
    public string PName 
          { get { return pName; } set { this.pName = value; } }
    public string TagNo 
          { get { return tagNo; } set { this.tagNo = value; } }
}



public class Service1 : IService1
{
    public List<WaitInfo> GetWaitingList()
    {
        MyDBDataContext db = new MyDBDataContext();

        var query = from w in db.WAIT_INFOs
                    select new WaitInfo 
                    { 
                        TagNo = w.PATIENT_INFO.TAG_NO, 
                        RoomName= w.ROOM_INFO.ROOM_NAME, 
                        PName= w.PATIENT_INFO.P_NAME 
                    };

        List<WaitInfo> result = query.ToList();

        return result;
    }

And following is codebehind part of UI layer which is provided by Silverlight

        public MainPage()
                {
                    InitializeComponent();

                    Service1Client s = new Service1Client();
                    s.GetWaitingListCompleted += 
    new EventHandler<GetWaitingListByCompletedEventArgs>( s_GetWaitingListCompleted);
                    s.GetWaitingListAsync();

                }

        void s_GetWaitingListCompleted(object sender,
     RadControlsSilverlightApplication1.ServiceReference2.GetWaitingListByCompletedEventArgs e)
                {

                    GridDataGrid.ItemsSource = e.Result;
                }


    And following is xaml code in Silverlight page


<Grid x:Name="LayoutRoot">
        <data:DataGrid x:Name="GridDataGrid"></data:DataGrid>
 </Grid>

It is very simple code, however what I am thinking weird is property name of object at "e.Result" in the code behind page.

In the service layer, although properties' names are surely "RoomName, PName, TagNo", in the silverlight properties' names are "roomName, pName, tagNo" which are private variable name of the WaitingList Object.

Did I something wrong?

Thanks in advance.


Unless you specifically decorate your class with the DataContract attribute (which you should, instead of Serializable) then a default DataContract will be inferred. For normal Serializable types, this means the fields will be serialized as opposed to the properties.

You can markup your class in either of the following two ways. The latter will use the property accessors when serializing/deserializing your object which may be very useful or be a hassle depending on your circumstances.

[DataContract]
public class WaitInfo
{
    [DataMember(Name="RoomName")]
    private string roomName;
    [DataMember(Name="PName")]
    private string pName;
    [DataMember(Name="TagNo")]
    private string tagNo;

    public string RoomName 
          { get { return roomName; } set { this.roomName = value; } }
    public string PName 
          { get { return pName; } set { this.pName = value; } }
    public string TagNo 
          { get { return tagNo; } set { this.tagNo = value; } }
}

The method I prefer:

[DataContract]
public class WaitInfo
{

    private string roomName;
    private string pName;
    private string tagNo;

    [DataMember]
    public string RoomName 
          { get { return roomName; } set { this.roomName = value; } }

    [DataMember]
    public string PName 
          { get { return pName; } set { this.pName = value; } }

    [DataMember]
    public string TagNo 
          { get { return tagNo; } set { this.tagNo = value; } }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜