gpt4 book ai didi

inheritance - 如何从WCF中未标记DataContractAttribute或SerializableAttribute的类继承?

转载 作者:行者123 更新时间:2023-12-03 15:02:50 44 4
gpt4 key购买 nike

在WCF中,如何从未标记DataContractAttribute或SerializableAttribute的类继承并将其用作datacontract?

例如:我有一个自定义类,名为“EmailAttachment”,该类是从System.Net.Mail.Attachment继承的:

[DataContract]
public class EmailAttachment : Attachment
{
[DataMember]
public string AttachmentURL
{
set;
get;
}
[DataMember]
public string DisplayName
{
set;
get;
}
}

但是,当我发布服务时,它抛出了运行时错误,说明:

System.Runtime.Serialization.InvalidDataContractException: Type 'EmailAttachment' cannot inherit from a type that is not marked with DataContractAttribute or SerializableAttribute.



有什么解决方法?

最佳答案

这取决于您希望数据契约(Contract)的外观。您不能使用包装器而不是派生类吗?就像是:

[DataContract]
public class EmailAttachment
{
public EmailAttachment(){}

internal EmailAttachment(Attachment inner) {_attachment = inner;}

[DataMember]
public string AttachmentURL
{
set;
get;
}
[DataMember]
public string DisplayName
{
set;
get;
}

private Attachment _attachment;
}

如果要在数据协定中公开内部System.Mail.Attachment的任何状态,则可以添加DataMember属性以显示该状态。

关于inheritance - 如何从WCF中未标记DataContractAttribute或SerializableAttribute的类继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4453947/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com