继承现有的.NET类的序列化错误 [英] Inheriting Existing .Net Class Serialization Error

查看:218
本文介绍了继承现有的.NET类的序列化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承和修改System.Net.MailMessage类为Web服务使用。我需要把它命名为MAILMESSAGE其他原因。当我使用这下面的code,我得到下面的错误。

类型System.Net.Mail.MailMessage'和'TestWebService.MailMessage都使用XML类型名称,'MAILMESSAGE,从命名空间http://tempuri.org/。使用XML属性可指定类型的唯一XML名称和/或命名空间。

我相信我要补充XMLRoot和类型的属性,但我似乎无法找出正确的组合。什么我需要做的,解决这个问题?

 命名TestWebService
{    [WebService的空间(namespace =htt​​p://tempuri.org/)]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)
    [System.ComponentModel.ToolboxItem(假)]
    公共类服务1:System.Web.Services.WebService
    {        [的WebMethod]
        公共字符串测试(MAILMESSAGE emailMessage)
        {
            返回成功了!
        }
    }
}命名空间TestWebService
{
    公共MAILMESSAGE类:System.Net.Mail.MailMessage
    {        MAILMESSAGE公():基地()
        {        }
    }
}


解决方案

您必须添加 XmlTypeAttribute 更改名称或命名空间,使其独特的serailization

 使用的System.Xml.Serialization[XmlType将(命名空间=htt​​p://tempuri.org/的TypeName =SomethingOtherThanMailMessage)]
公共MAILMESSAGE类:System.Net.Mail.MailMessage
{
}

然而, System.Net.Mail.MailMessage 本身不serailizable这样就从它派生类不会序列化。

I am inheriting and modifying the System.Net.MailMessage class for use in a web service. I need to keep it named MailMessage for other reasons. When I use this in the code below, I get the error below.

"Types 'System.Net.Mail.MailMessage' and 'TestWebService.MailMessage' both use the XML type name, 'MailMessage', from namespace 'http://tempuri.org/'. Use XML attributes to specify a unique XML name and/or namespace for the type."

I belive I have to add XMLRoot and Type attributes, but I can't seem to figure out the right combination. What do I need to do to resolve this error?

namespace TestWebService
{

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string Test(MailMessage emailMessage) 
        {
            return "It Worked!";
        }
    }
}

namespace TestWebService 
{
    public class MailMessage : System.Net.Mail.MailMessage
    {

        public MailMessage() : base()
        {

        }
    }
}

解决方案

You have to add XmlTypeAttribute to change the name or namespace to make it unique for serailization

using System.Xml.Serialization

[XmlType(Namespace = "http://tempuri.org/", TypeName = "SomethingOtherThanMailMessage")]
public class MailMessage : System.Net.Mail.MailMessage
{
}

However, System.Net.Mail.MailMessage itself is not serailizable so your class that is derived from it won't be serializable.

这篇关于继承现有的.NET类的序列化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆