如何创建现有对象的新版本 [英] How do I create a new version of an existing object
问题描述
我有几个继承自 BaseMessage
类的类继承自 IMessage
界面。有一个名为 Requeue()
的 BaseMessage
方法。此方法应使用新的到期
日期时间创建新版本的继承消息。
问题是 Requeue()
方法不知道它自己的类是什么。
我试过以下内容:
Hi,
I have several classes that inherit from an abstract BaseMessage
class which inherits from the IMessage
interface. There is a BaseMessage
method called Requeue()
. This method should create a new version of the inherited message with a new Due
datetime.
The issue is that the Requeue()
method has no idea what it's own class is.
I tried the following:
public IMessage Requeue()
{
var message = this;
//set the shared field values
...
//Serialize it up (also a BaseMessage method)
message.SerializedData = Serialize();
message.Save();
return message;
}
这不起作用。似乎var消息只是成为原始对象的引用,这会破坏东西。
然后我尝试了
This doesn't work. It appears that the var message just becomes a ref to the original object, which breaks stuff.
So then I tried
public static IMessage Requeue(IMessage m)
{
var message = m;
//etc
...
}
我认为 m
将按值传递,因为它是c#默认,但我有同样的问题。
所以最终我将 BaseMessage
改为非抽象(什么是为此工作?)所以我至少可以通过使用以下方法假装:
I figured that m
would be passed by value as that's the c# default, but I have the same problem.
So eventually I changed BaseMessage
to non-abstract (what's the work for that?) so I could at least "fake it" by using the following:
public IMessage Requeue()
{
IMessage message = new BaseMessage();
//blah
...
//fake the class type here. It's the place it is really needed
message.SerializedData = Serialize();
message.Save();
return message;
}
目前的问题是保存()
还使用 Serialize()
我想我可以得到另一个 IMessage
在我 Save()
之前使用 DeSerialize()
但感觉就像我在沿着线路进一步追逐解决方案比我需要的更多。
我想让这些类尽可能简单,并将尽可能多的代码放在 BaseMessage
尽可能抽象,因为我想把项目交给一个大三来维护。添加新消息并将其作为插件上传应该成为标准做法。
有没有一种方法可以克隆原始消息而不提及原始消息?我确定我应该知道这个知道> _<< br mode =hold/>
谢谢^ _ ^
Andy
The current issue is that Save()
also uses Serialize()
I figured I could get another IMessage
by using DeSerialize()
before I Save()
but it feels like I'm chasing down the solution further along the line than I need to.
I want to keep these classes as simple as possible and put as much of the code in the BaseMessage
abstract as I can because I want to hand the project over to a junior to maintain. Adding new messages and uploading them as plugins should become standard practice.
Is there a way I can "Clone" the original message without there being a reference to the original? I'm sure I should know this by know >_<<br mode="hold" />
Thanks ^_^
Andy
推荐答案
如何添加abstract虚拟方法来创建克隆对象?
How about adding aabstractvirtual method to create the cloned object?
protected virtual IMessage Clone()
{
return (IMessage)Activator.CreateInstance(GetType());
}
public IMessage Requeue()
{
var message = Clone();
if (ReferenceEquals(message, this))
{
throw new InvalidOperationException("Clone must return a different object!");
}
//set the shared field values
...
//Serialize it up (also a BaseMessage method)
message.SerializedData = Serialize();
message.Save();
return message;
}
这篇关于如何创建现有对象的新版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!