using System; using System.Collections.Generic; namespace JD.Serialization { public partial class ObjectFactory { private readonly Dictionary objects = new Dictionary(); static ObjectFactory() { init(); } public Type getTypeFromStringID(StringID id) { return getTypeFromStringID(id.getValue()); } public Type getTypeFromStringID(uint id) { if (objects.ContainsKey(id)) return objects[id]; else return null; } public uint getStringIDFromType(Type _t) { foreach (KeyValuePair pair in objects) if (_t == pair.Value) return pair.Key; return uint.MaxValue; } public void addType(StringID id, Type type ) { addType(id.getValue(), type); } public void addType(uint id, Type type) { if (!objects.ContainsKey(id)) { objects.Add(id, type); if (type.BaseType != null && type.BaseType != typeof(object)) { addType((new StringID(type.BaseType.Name)), type.BaseType); } } } bool isTypeInFactory(Type type) { return objects.ContainsValue(type); } protected static List factories = new List(); static public List getFactories() { return factories; } public static void addFactory(ObjectFactory factory) { factories.Add(factory); } public static bool isTypeInAnyFactory(Type type) { foreach (ObjectFactory objFact in factories) { if (objFact.isTypeInFactory(type)) return true; } return false; } public static ObjectFactory getFactoryForType(Type type) { foreach (ObjectFactory objFact in factories) { if (objFact.isTypeInFactory(type)) return objFact; } return null; } public Type[] getInstanciableTypes() { Type[] types = new Type[objects.Count]; objects.Values.CopyTo(types, 0); return types; } public static Type getTypeFrom(StringID id) { return getTypeFrom(id.getValue()); } public static Type getTypeFrom(uint id) { foreach (ObjectFactory objFact in factories) { Type obj = objFact.getTypeFromStringID(id); if (obj != null) return obj; } return null; } public static uint getStringIDFrom(Type _t) { foreach (ObjectFactory objFact in factories) { uint strId = objFact.getStringIDFromType(_t); if (strId != uint.MaxValue) return strId; } return uint.MaxValue; } } }