任何人都可以建议一个干净的方法来做到这一点。我想要一个带有游戏中所有实例化对象的词典的世界。但我不确定如何正确行事。
对象应该都在1个伞形对象和1个dict中,以便于保存/搜索。
//all connected players
private Dictionary<IOClient, string> IOClients = new Dictionary<IOClient, string>();
player = new MObject(new object[]{result.Remove(0,1)}, 1);
public class MObject
{
private Serial m_Serial;
public Serial Serial { get { return m_Serial; } }
public Player player = null;
public Item item = null;
public MObject(object[] obj, int type)
{
// m_Serial = Serial.NewMobile;
if (obj.GetType() == typeof(Player) || type == 1)
{
player = new Player((string)obj[0]);
}
if (obj.GetType() == typeof(Item) || type == 2)
{
// item = new Item();
}
}
}
//Also a World
public static class World
{
private static bool m_Loading;
private static bool m_Loaded;
private static Dictionary<Serial, MObject> m_MObjects;
public static Dictionary<Serial, MObject> MObjects
{ get { return m_MObjects; } }
}
答案 0 :(得分:0)
听起来这里的核心问题是强烈打字的弱点之一。
一种可能的解决方案是为对象引入接口,并为每个接口提供一个或多个字典。
e.g
您拥有所有IDrawable对象的字典,所有ICanShoot对象的字典,所有ICanBeShot对象的字典等等。
这样,每次你使用其中一个词典时,你都不需要施放...而且你可以获得额外的松耦合等奖励。
如果您想进一步扩展这个想法,可以将字典包装到IOC容器中......但这本身就是一个完整的主题。