POCO 示例演示了对未标记类型的序列化支持;也就是说,序列化属性尚未应用到的类型,有时称为普通旧 CLR 对象 (POCO) 类型。 DataContractSerializer 为所有具有无参数构造函数的公共未标记类型推断一个数据协定。 数据协定允许你向/从服务传递结构化数据。 有关未标记的类型的详细信息,请参阅 可序列化类型。
此示例基于 入门,但使用复数而不是基元数值类型。 它也类似于 基本数据协定 示例,只不过不使用 DataContractAttribute 和 DataMemberAttribute 属性。
该服务由 Internet Information Services(IIS)托管,客户端是控制台应用程序(.exe)。
注释
本示例的设置过程和生成说明位于本主题末尾。
类 ComplexNumber
用于 ServiceContract
. 该 ComplexNumber
类型没有 DataContractAttribute 属性和 DataMemberAttribute 属性,如以下示例代码所示。 默认情况下,将序列化所有公共属性和字段。
public class ComplexNumber
{
public double Real;
public double Imaginary;
public ComplexNumber()
{
Real = double.MinValue;
Imaginary = double.MinValue;
}
public ComplexNumber(double real, double imaginary)
{
this.Real = real;
this.Imaginary = imaginary;
}
}
设置、生成和运行示例
确保已为 Windows Communication Foundation 示例 执行One-Time 安装过程。
若要生成解决方案的 C# 或 Visual Basic .NET 版本,请按照 生成 Windows Communication Foundation 示例中的说明进行操作。
若要在单台计算机或跨计算机配置中运行示例,请按照 运行 Windows Communication Foundation 示例中的说明进行操作。