在 .NET / C# 中克隆或使 MailMessage 不可变

发布于 2024-10-20 19:26:43 字数 365 浏览 6 评论 0原文

我正在编写一个小型库,在其中编写一些接口,这些接口接受 MailMessage 对象并使用单个发送方法将类返回给我,该发送方法抽象了 MailMessage 的发送方式。我可以控制将进行实际发送的类,但我无法控制实施策略类。我想防止任何实施策略类更改 MailMessage。如何使 MailMessage(及其包含的集合)不可变或创建它的克隆?我在 MailMessage 本身上没有看到 .Clone 方法,就像在其他类上看到的那样。我是否必须新建一个新的 MailMessage 对象并对该新的 MailMessage 进行逐个属性设置?

请注意,我主要是一名 Java 人员,正在帮助客户使用 C# 2.0 代码,因此我对此类事情的 C# / .NET 习惯用法有点不熟悉。任何帮助表示赞赏。

I'm writing a small library where I'm writing some interfaces that takes a MailMessage object and returns a class back to me with a single send method that abstracts away how that MailMessage will be sent. I have control over the class that will do the actual sending, but I don't have control over the implementing strategy classes. I'd like to prevent any implementing strategy classes from altering the MailMessage. How can I make the MailMessage (and the collections it contains) immutable or create a clone of it? I don't see a .Clone method on MailMessage itself like I do on other classes. Will I have to result to new'ing up a new MailMessage object and doing a property-by-property set on that new MailMessage?

Note, I'm mostly a Java guy and am helping a client with C# 2.0 code, so I'm slightly unfamiliar with C# / .NET idioms for things like this. Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玻璃人 2024-10-27 19:26:43

我认为对此没有快速的解决方案。 MailMessage 本质上是可变的,并且不适合克隆或序列化/反序列化。如果您需要确保没有人可以更改它,请不要缓存它,而是定义一个类来保存消息值的副本,并从此实例中创建新的 MailMessage 实例。

I don't think there is a quick solution for this. The MailMessage is inheritly mutable and isn't designed to be cloned or serialized/deserialized. If you need to make sure nobody can change it, don't cache it, but define a class that holds a copy of the values of the message and make new MailMessage instances out of this instance.

饭团 2024-10-27 19:26:43

我知道这是一个老问题,但我最近不得不实现这个,所以这就是我所做的。 这是一个浅克隆,尽管需要做更多的工作,它可以变成一个深层克隆。

        private MailMessage MailMessageShallowCopy(MailMessage original)
    {
        string to = original.To.ToString();
        string from = original.From.ToString();
        string subject = original.Subject;
        string body = original.Body;
        string cc = original.CC.ToString();
        string bcc = original.Bcc.ToString();

        MailMessage clone = new MailMessage(from, to, subject, body);
        if (!string.IsNullOrEmpty(cc))
        {
            clone.CC.Add(cc);
        }
        if (!string.IsNullOrEmpty(bcc))
        {
            clone.Bcc.Add(bcc);
        }
        foreach (AlternateView view in original.AlternateViews)
        {
            var clonedView = new AlternateView(view.ContentStream);
            foreach (var resource in view.LinkedResources)
            {
                clonedView.LinkedResources.Add(resource);
            }
            clone.AlternateViews.Add(clonedView);
        }
        foreach (Attachment attachment in original.Attachments)
        {
            clone.Attachments.Add(attachment);
        }
        clone.BodyEncoding = original.BodyEncoding;
        clone.DeliveryNotificationOptions = original.DeliveryNotificationOptions;
        foreach (NameValueCollection header in original.Headers)
        {
            clone.Headers.Add(header);
        }
        clone.HeadersEncoding = original.HeadersEncoding;
        clone.IsBodyHtml = original.IsBodyHtml;
        clone.Priority = original.Priority;
        foreach (MailAddress mailAddress in original.ReplyToList)
        {
            clone.ReplyToList.Add(mailAddress);
        }
        clone.Sender = original.Sender;
        clone.SubjectEncoding = original.SubjectEncoding;
        return clone;
    }

I know this is an old question, but I recently had to implement this so here's what I did. This is a shallow clone, though with a little more work it could be made into a deep clone.

        private MailMessage MailMessageShallowCopy(MailMessage original)
    {
        string to = original.To.ToString();
        string from = original.From.ToString();
        string subject = original.Subject;
        string body = original.Body;
        string cc = original.CC.ToString();
        string bcc = original.Bcc.ToString();

        MailMessage clone = new MailMessage(from, to, subject, body);
        if (!string.IsNullOrEmpty(cc))
        {
            clone.CC.Add(cc);
        }
        if (!string.IsNullOrEmpty(bcc))
        {
            clone.Bcc.Add(bcc);
        }
        foreach (AlternateView view in original.AlternateViews)
        {
            var clonedView = new AlternateView(view.ContentStream);
            foreach (var resource in view.LinkedResources)
            {
                clonedView.LinkedResources.Add(resource);
            }
            clone.AlternateViews.Add(clonedView);
        }
        foreach (Attachment attachment in original.Attachments)
        {
            clone.Attachments.Add(attachment);
        }
        clone.BodyEncoding = original.BodyEncoding;
        clone.DeliveryNotificationOptions = original.DeliveryNotificationOptions;
        foreach (NameValueCollection header in original.Headers)
        {
            clone.Headers.Add(header);
        }
        clone.HeadersEncoding = original.HeadersEncoding;
        clone.IsBodyHtml = original.IsBodyHtml;
        clone.Priority = original.Priority;
        foreach (MailAddress mailAddress in original.ReplyToList)
        {
            clone.ReplyToList.Add(mailAddress);
        }
        clone.Sender = original.Sender;
        clone.SubjectEncoding = original.SubjectEncoding;
        return clone;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文