using System; using System.Collections.Generic; using System.Reflection; using Common.Base; namespace Model { public class FactoryComponent : Component, IAssemblyLoader where T : Entity { private Dictionary> allConfig; public void Load(Assembly assembly) { allConfig = new Dictionary>(); Type[] types = assembly.GetTypes(); foreach (Type type in types) { object[] attrs = type.GetCustomAttributes(typeof(FactoryAttribute), false); if (attrs.Length == 0) { continue; } FactoryAttribute attribute = (FactoryAttribute)attrs[0]; if (attribute.ClassType != typeof (T)) { continue; } object obj = (Activator.CreateInstance(type)); IFactory iFactory = obj as IFactory; if (iFactory == null) { throw new Exception( string.Format("class: {0} not inherit from IFactory", type.Name)); } allConfig[attribute.Type] = iFactory; } } public T Create(int configId) { int type = (int) World.Instance.GetComponent().Get(configId).Type; return this.allConfig[type].Create(configId); } } }