【发布时间】:2017-02-26 02:23:41
【问题描述】:
我正在学习 wcf,我看到这个选择加入和退出序列化。我还在挠头。我已经看过this SO post。但这没有帮助。谁能简要解释一下它是什么?
【问题讨论】:
标签: wcf serialization
我正在学习 wcf,我看到这个选择加入和退出序列化。我还在挠头。我已经看过this SO post。但这没有帮助。谁能简要解释一下它是什么?
【问题讨论】:
标签: wcf serialization
其实很简单: Opt-In 方法表示必须明确标记被视为 DataContract 一部分的属性,否则将被忽略,而 Opt-Out 意味着除非明确标记,否则所有属性都将被假定为 DataContract 的一部分。
namespace MySchoolService
{
[DataContract]
public class Student
{
[DataMember]
public string StudentNumber;
[DataMember]
public string FirstName;
[DataMember]
public string LastName;
public string MarksObtained;
}
[ServiceContract]
public interface IStudentService
{
//Service Code Here.
}
}
在上面的代码中,Student 类的StudentNumber、FirstName、LastName 属性被显式标记为DataMember 属性,与MarksObtained 相对,因此MarksObtained 将被忽略。
下面的代码代表了一个选择退出方法的示例。
namespace MySchoolService
{
[Serializable()]
public class Student
{
public string StudentNumber;
public string FirstName;
public string LastName;
[NonSerialized()]
public string marksObtained;
}
[ServiceContract]
public interface IStudentService
{
//Service Code Here.
}
}
在上面的示例中,我们将MarksObtained 属性显式标记为[NonSerialized()] 属性,因此除了其他属性外,它将被忽略。
希望能帮到你。
【讨论】: