简体   繁体   English

WCF和[DataMember]属性

[英]WCF and the [DataMember] Attribute

I have the following (abbreviated) class that is sent and received to/from the client via WCF: 我有以下(缩写)类通过WCF发送到客户端/从客户端接收:

public class Sparetime : ChartConfigurationBase, IChartConfiguration
{
    [DataMember]
    public int SparetimeConfigurationId { get; set; }

    public Single FeederOffRate { get; set; }
}

Notice the first property uses the DataMember attribute and the second doesn't. 请注意,第一个属性使用DataMember属性,第二个属性不使用。 Am I correct that only the first property would get serialized and sent to the client when a WCF call is made? 我是否更正,在进行WCF调用时,只有第一个属性会被序列化并发送给客户端?

Yes, you are right, the MSDN documentation specifies it : 是的,你是对的, MSDN文档指定它:

When applied to the member of a type, specifies that the member is part of a data contract and is serializable by the DataContractSerializer. 应用于类型成员时,指定该成员是数据协定的一部分,并且可由DataContractSerializer序列化。

You should add DataContract attribute to your class to make it serializable : 您应该将DataContract属性添加到您的类中以使其可序列化:

[DataContract]
public class Sparetime : ChartConfigurationBase, IChartConfiguration
{
}

Note that FeederOffRate will be set to its default value (null for reference types). 请注意, FeederOffRate将设置为其默认值(引用类型为null)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM