简体   繁体   English

无法实现IClonable或ISerializable的C#克隆对象

[英]C# Cloning Objects that implement neither IClonable or ISerializable

I've tried everything I can think of and cannot figure this out. 我已经尝试了所有我能想到的并且无法解决的问题。 Basically, I'm making an Outlook 2010 Add-In that makes adjustments to incoming HTML formatted emails for the purposes of making them more Accessible to visually-impaired users (visually impaired users in particular.) 基本上,我正在制作一个Outlook 2010加载项,它对传入的HTML格式的电子邮件进行了调整,目的是使它们对视障用户(尤其是视障用户)更易于访问。

Everything works fine, except that when my Add-In runs, it actually attempts to modify the original email no matter what I try, which Exchange connected Outlook doesn't like at all and rejects. 一切正常,除了运行我的加载项时,无论我尝试什么,它实际上都尝试修改原始电子邮件,哪个与Exchange连接的Outlook根本不喜欢并拒绝。 What I want to do is display my modified email message with all of the goodies (reply, reply-all, BCC, etc., just like you would normally use Outlook) without modifying the original message...that is, I only want to display my modified message, not modify the stored message or add a new message. 我想要做的是显示修改过的电子邮件,其中包含所有好东西(答复,全部答复,密件抄送等),就像您通常使用Outlook一样,而无需修改原始消息……也就是说,我只想要显示我修改过的消息,而不修改存储的消息或添加新消息。

Such as: 如:

if (selObject is Outlook.MailItem)
{
    Outlook.MailItem mailItem = (selObject as Outlook.MailItem);
    Outlook.MailItem accessible_mail_item = mailItem;
    ...rest of my code...
    accessible_mail_item.Display(false);
}

The problem I have is, "accessible_mail_item" is not a copy of "mailItem"...it's a pointer to it. 我的问题是,“ accessible_mail_item”不是“ mailItem”的副本...而是指向它的指针。 How can I make a copy/clone of this non-Serializable/Clonable Object? 如何制作此不可序列化/可克隆对象的副本/克隆? I've tried various "Deep Clone" functions out there, but they all give me the same errors about "not being serializable" or whatever. 我已经尝试过各种“深度克隆”功能,但是它们都给我有关“不可序列化”或类似错误的错误。

Is there any reason why you can't use the MailItem.Copy method? 有什么原因不能使用MailItem.Copy方法?

Outlook.MailItem mailItem = (selObject as Outlook.MailItem);
Outlook.MailItem accessible_mail_item = mailItem.Copy();
...rest of my code...
accessible_mail_item.Display(false);

Sounds like you've been trying Deep Clone methods which are serializing the object out and then deserializing back into a new object (using BinaryFormatter or similar), thus creating the clone. 听起来您一直在尝试Deep Clone方法,该方法先将对象序列化,然后反序列化回新对象(使用BinaryFormatter或类似方法),从而创建克隆。 This of course, requires that the objects you are cloning be Serializable. 当然,这要求您要克隆的对象可序列化。

Have you tried any other approaches to deep cloning? 您是否尝试过其他克隆方法? (eg, Reflection, IL, ExpressionTrees). (例如,Reflection,IL,ExpressionTrees)。 Using any of these should be magnitudes faster than the serialization approach, and won't require that the objects be marked up with Serializable, or implement ICloneable. 使用这些方法中的任何一个都应该比序列化方法快得多,并且不需要将对象标记为Serializable或实现ICloneable。

Try here for an IL deep clone implementation, or here for Reflection, Expression Tree approaches. 在此处尝试进行IL深度克隆实现, 在此处尝试进行反射,表达式树方法。

If class is not designed to support copy/clone operation it is very unlikely you'll be able to come up with code that will produce functional clone. 如果该类不是为支持复制/克隆操作而设计的,那么您将不太可能提出能够产生功能性克隆的代码。 This is especially true for objects that represent different external entities ore resources. 对于表示不同外部实体矿石资源的对象尤其如此。 Ie would you expect to create copy of a remote server page by cloning HttpResponse or creating duplicate SQL server by cloning corresponding managed object? 即您希望通过克隆HttpResponse来创建远程服务器页面的副本,还是通过克隆相应的受管对象来创建重复的SQL Server? The same with Outlook messages - object that represent mail item to managed code is not actual mail item, but rater way to obtain/modify one somewhere. 与Outlook邮件相同-将邮件项目表示为托管代码的对象不是实际的邮件项目,而是在某处获取/修改邮件的评级方法。

I would recommend to look through Outlook API and see if there is a way to create copy of an item the way you want it. 我建议您浏览一下Outlook API,看看是否有一种方法可以按照您想要的方式创建项目的副本。 You may also try to create new mail item via Outlook APIs and than manually copy properties you are interested in. 您还可以尝试通过Outlook API创建新的邮件项目,然后手动复制您感兴趣的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM