在WCF中使用new关键字隐藏基类属性



我在我的服务器DLL类库中有一个这样的场景。

 [DataContract]
public class Base
{
    [DataMember]
    public string Info { get; set; }
}
[DataContract]
public class Child : Base
{
    [DataMember]
    public new int Info { get; set; }

    public int Save()
    { 
    }
}

我的WCF代理在客户端创建一个引用类。将"Info"重命名为"Info1"。并在基类中显示适当的属性。我的代码编译得很好。到目前为止一切顺利。当我尝试从客户端运行ChildProxy.Save()时,它会给我错误提示

"试图序列化参数http://tempuri.org/:info时发生错误。日志含义InnerException消息为Type clientservicellayer . infoservice。不需要包含数据合约名称为"ArrayOfInfo:http://schemas.datacontract.org/2004/07/Info_DLL"的信息。将任何未知的静态类型添加到已知类型列表中—例如,通过使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知类型列表中。详情请参见InnerException。"

如何在WCF中隐藏基类的属性?

更新:

下面是客户端的调用

InvoiceServiceClient infoProxy = new InfoServiceClient(); 
invId = invfoProxy.Save();

如下所述:

你不能。虽然子类"隐藏"了基类的Info属性,但序列化器正在读取该属性。

你可以尝试添加[DataMember(Name = "Info")]到子类,看看会发生什么

您可以在DataContract

中使用KnownType Attribute

MSDN数据合同已知类型

最新更新