我不完全知道如何解决我遇到的这类问题。
private Dictionary<int, Tire> m_vehicleTireSelected = new Dictionary<int, Tire>()
{
{0, new TireCasual()
{
Name = "Monster Tire",
Position = new Vector3(-0.94f, -1.09f)
}},
{1, new TireMonster()
{
Name = "Casual Tire",
Position = new Vector3(1.05f, -1.09f)
}}
};
public void ChangeTire(int tireIndex, int tireKey)
{
m_bus.ChangeTire(tireIndex, m_vehicleTireSelected[tireKey]);
}
所以我想在这里使用例如Dictionary来存储一些轮胎模板对象,以后再用新的对象进行更改。这里的问题是,当我从字典中分配轮胎时,它仍然是同一轮胎,因为它是引用类型变量,但最终我希望它是COPY。有人可以帮助我,也许可以提出一个解决方案吗?我还应该提到这是性能关键部分。
1 000 000 objects composed of 4 ints
Binary Serializer : 10.361799 seconds.
MemberwiseClone : 0.080879 seconds. (128x gain)
1 000 000 objects composed of 4 Lists<int> with 4 items (16 ints + 4 List)
Binary Serializer : 47.288164 seconds.
MemberwiseClone : 0.517383 seconds. (91x gain)
PS:您可能已经注意到我使用自己的接口而不是System.ICloneable。这是因为当通用名称不可用时,.NET 2.0时代的内置接口已经存在。它也有一个主要警告,因为它没有正确说明其意图。基本上,没有办法知道Clone方法会带来什么。它是浅表副本还是深表副本,甚至是同一类型吗?无法确定。这就是为什么我建议实现自己的IDeepCloneable和IShallowCloneable接口的原因。