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) { this.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)); var iFactory = obj as IFactory; if (iFactory == null) { throw new Exception(string.Format("class: {0} not inherit from IFactory", type.Name)); } this.allConfig[attribute.Type] = iFactory; } } public T Create(int type, int configId) { return this.allConfig[type].Create(configId); } } }