C# 克隆 - 使用不可序列化的数据类型
在我的游戏引擎中实现对象克隆的过程中,我遇到了一些障碍。 我的目标是拥有一个克隆系统,我不必逐个班级进行维护,除非该班级需要特殊对待。
我的游戏引擎的设置围绕基类 Object2D 展开,其中包含一些 Texture2D 形式的图像数据。好吧,长话短说,它包含一个 DisplayObject,其中包含一个 Sprite,其中又包含一个 Texture2D。当然,其他类,例如“Player”、“Enemy”、“Projectile”等,都派生自基本的 Object2D 类。
不幸的是,我发现XNA的Texture2D类不可序列化。这是有道理的,因为我们不想在内存中随意复制纹理数据。
这给我带来了两难的境地。我使用深度克隆方法来克隆对象,但由于它不可序列化,我无法再这样做。我尝试只标记Texture2D [NonSerialized],但是当我尝试绘制时这会导致问题,因为克隆的纹理为空。我无法找到任何允许我在克隆后分配它的隐藏技巧(例如某种“onClone()”方法)。
所以我想我会这样做。对于一般无法深度克隆的对象,我实现了一个“SpecialClone”接口,它允许我指定一个clone()方法。
但是,因为一般无法深度克隆的类是基类,所以我又回到了开始的地方:逐个类地编写克隆方法。
public static T clone<T>(T obj) {
if (obj == null) return default(T);
if (!typeof(T).IsSerializable) {
if (obj is SpecialClone) {
object obj2 = ((SpecialClone)obj).clone();
return (T)obj2;
} else {
throw new ArgumentException("Object type is not serializable.", "obj type: " + Type.GetTypeHandle(obj));
}
} else {
return deepClone(obj);
}
}
public static T deepClone<T>(T obj) {
if (obj == null) return default(T);
if (typeof(T).IsSerializable) {
try {
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
ms.Seek(0, SeekOrigin.Begin);
T obj2 = (T)bf.Deserialize(ms);
ms.Dispose();
return obj2;
}
catch (Exception e) {
Console.WriteLine(e);
return default(T);
}
} else {
Console.WriteLine(typeof(T) + " is not marked as serializable!");
return default(T);
}
}
最好还是用 C#,所以我不确定我是否错过了一些可以使用的技巧,或者这是否真的是我处理这个问题的方式。这种克隆的主要目的是针对 Object2D 类型,因此必须为我创建的每个新子类编写克隆方法确实很痛苦。
有什么办法可以避免这种情况吗?
I've hit a bit of a stumbling block in my quest to implement object cloning in my game engine.
My goal is to have a cloning system that I don't have to maintain on a class by class basis, unless the class needs special treatment.
My game engine's setup revolves around a base class Object2D, which contains some image data in the form of a Texture2D. Well, the long story is that it contains a DisplayObject, which contains a Sprite, which contains a Texture2D. Naturally, other classes e.g. "Player", "Enemy", "Projectile", etc. all derive from the basic Object2D class.
Unfortunately, I found that XNA's Texture2D class isn't serializable. Which makes sense, as we wouldn't want to be duplicating texture data in memory all willy-nilly.
This creates a dilemma for me. I was using deep cloning methods to clone objects, but since it's not serializable I can no longer do that. I tried just marking the Texture2D [NonSerializable], but this causes problems when I try to draw, as the clone's texture is null. I wasn't able to find any hidden trick that allows me to assign it after cloning (like some sort of an "onClone()" method).
So I figured I'd do this. For objects that can't be deep cloned generically, I implement a "SpecialClone" interface that allows me to specify a clone() method.
However, because the class that can't be deep cloned generically is the base class, I'm right back where I started: writing a clone method on a class by class basis.
public static T clone<T>(T obj) {
if (obj == null) return default(T);
if (!typeof(T).IsSerializable) {
if (obj is SpecialClone) {
object obj2 = ((SpecialClone)obj).clone();
return (T)obj2;
} else {
throw new ArgumentException("Object type is not serializable.", "obj type: " + Type.GetTypeHandle(obj));
}
} else {
return deepClone(obj);
}
}
public static T deepClone<T>(T obj) {
if (obj == null) return default(T);
if (typeof(T).IsSerializable) {
try {
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
ms.Seek(0, SeekOrigin.Begin);
T obj2 = (T)bf.Deserialize(ms);
ms.Dispose();
return obj2;
}
catch (Exception e) {
Console.WriteLine(e);
return default(T);
}
} else {
Console.WriteLine(typeof(T) + " is not marked as serializable!");
return default(T);
}
}
I'm not the best with C# yet, so I'm not sure if I'm missing some trick that I can use or if this is really how I'll have to deal with this problem. The main purpose of this cloning is for Object2D types, so it'd be a real pain to have to write clone methods for each new subclass I make.
Is there any way to avoid this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不想克隆Texture2d。
您应该将纹理(以及模型、波形文件等...)视为“共享资源”,它可以有很多大小为兆字节。克隆会让你陷入内存不足的世界,很快就会受到伤害(除了克隆所需的时间之外)
你的理想解决方案:相反,你想要克隆参考。
如果每个 Object2D 实例不打算被序列化,那么您可以将相同的 Texture2D 引用分配给各个 Object2D。如果要序列化,请如上所述传递Texture2D引用,并复制字符串textureFilePath,这就是序列化的内容。
另请注意,这是 XNA 的 ContentManager 自动为您执行的操作:如果您请求“myPic.xnb”10 次,它会自动加载一次并将该引用返回给您 10 次。
you do not want to clone the Texture2d.
You should consider Textures (and models, wave files, etc...) as a "shared asset", it could be many megabytes in size. Cloning that would leave you in a world of out-of-memory hurt real quick (in addition to the time it takes to clone)
your ideal solution: instead, you want to clone the reference.
if each of your Object2D instances are not meant to be serialized, then you can just reference-assign the same Texture2D to the various Object2D's. If you want to serialize, then pass the Texture2D reference, as mentioned, plus copy a string textureFilePath, which is what gets serialized.
Also note that this is what XNA's ContentManager does for you automatically: if you request "myPic.xnb" 10 times, it will automagically load it once and return that reference to you 10 times.
ICloneable(.Net核心接口)比SpecialClone更好。您还可以使用受保护的构造函数来实现 ISerialized 接口
ctor(SerializationInfo 信息,StreamingContext 上下文)。
这将允许自定义序列化(SerializationInfo 是一个包,可以容纳稍后反序列化对象所需的任何内容,并在受保护的构造函数中传递给您)。
这就是关于序列化的全部知识。如果你能做到这一点,取决于不可序列化类中是否有一些东西(即键、文件名、缓冲区),你可以在反序列化时使用它来重新创建该对象。
ICloneable (.Net core interface) is better than SpecialClone. You could also implement the ISerializable interface allong with a protected constructor
ctor(SerializationInfo info, StreamingContext context).
This would allow for custom serialization (SerializationInfo is a bag that can hold anything you need to deserialize the object later and is passed to you in the protected constructor).
So thats all there is to know about serialization. If you can do it or not depends on whether there is something in the non-serializable class (i.e. a key, a filename, a buffer) which you can use on deserialization to recreate that object.