Ejemplo n.º 1
0
        private void SetDependencies()
        {
            foreach (ComponentDescription componentDescription in _components)
            {
                foreach (AssemblyName referencedAssemblyName in componentDescription.Assembly.GetReferencedAssemblies())
                {
                    Assembly referencedAssembly = Assembly.Load(referencedAssemblyName);
                    List <ComponentDescription> dependedComponentList = _components.Where(m => m.Assembly == referencedAssembly).ToList();
                    if (dependedComponentList.Count > 0)
                    {
                        componentDescription.Dependencies.AddRange(dependedComponentList);
                    }
                }

                foreach (Type dependedComponentType in ComponentBase.FindDependedComponentTypes(componentDescription.Type))
                {
                    ComponentDescription dependedComponent = _components.FirstOrDefault(m => m.Type == dependedComponentType);
                    if (dependedComponent == null)
                    {
                        throw new Exception(string.Format("未找到组件 {1} 依赖的组件 {0}。", dependedComponentType.AssemblyQualifiedName, componentDescription.Type.AssemblyQualifiedName));
                    }

                    if ((componentDescription.Dependencies.FirstOrDefault(dm => dm.Type == dependedComponentType) == null))
                    {
                        componentDescription.Dependencies.Add(dependedComponent);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 加载所有组件。
        /// </summary>
        private void LoadAllComponents()
        {
            ICollection <Type> componentTypes = AddMissingDependedComponents(_componentFinder.FindAll());

            foreach (Type componentType in componentTypes)
            {
                if (!ComponentBase.IsComponent(componentType))
                {
                    throw new Exception(string.Format("{0} 不是 Ls 组件。", componentType.AssemblyQualifiedName));
                }

                if (!_iocManager.IsRegistered(componentType))
                {
                    _iocManager.Register(componentType);
                }
            }

            foreach (Type componentType in componentTypes)
            {
                ComponentBase componentObject = (ComponentBase)_iocManager.Resolve(componentType);
                componentObject.IocManager = _iocManager;
                _components.Add(new ComponentDescription(componentObject));
            }

            int coreComponentIndex = _components.FindIndex(m => m.Type == typeof(LsCoreComponent));

            if (coreComponentIndex > 0)
            {
                ComponentDescription coreComponent = _components[coreComponentIndex];
                _components.RemoveAt(coreComponentIndex);
                _components.Insert(0, coreComponent);
            }

            SetDependencies();
        }