/// <summary> /// 扫描并获取当前系统的准备器 /// </summary> /// <returns></returns> public static List <IAutofacContainerPrepare> GetPrepares() { List <IAutofacContainerPrepare> returnValue = new List <IAutofacContainerPrepare>(); var allAssemblies = AppDomain.CurrentDomain.GetAssemblies(); //循环程序集 foreach (var oneAssemblies in allAssemblies) { List <Type> tempTypes = new List <Type>(); //获取类型 try { tempTypes = oneAssemblies.GetTypes().ToList(); } catch (Exception) { continue; } //循环类型 foreach (var oneType in tempTypes) { //若派生体系不符合要求 if (!m_useBaseContextType.IsAssignableFrom(oneType)) { continue; } if (null != oneType.GetCustomAttribute(m_useAttributeType)) { var tempAttribute = oneType.GetCustomAttribute(m_useAttributeType) as AutoContextAttribute; IKEYFilter tempFilter = null; tempFilter = CreateKeyFilter(tempAttribute); IAutofacContainerPrepare tempPrepare = MakePrepare(oneType, tempFilter); if (null != tempPrepare) { returnValue.Add(tempPrepare); } } } } return(returnValue); }
/// <summary> /// 制作准备器 /// </summary> /// <param name="oneType"></param> /// <param name="tempFilter"></param> /// <returns></returns> private static IAutofacContainerPrepare MakePrepare(Type oneType, IKEYFilter tempFilter) { IAutofacContainerPrepare tempPrepare = null; try { //制作泛型类型 var tempType = m_useBasePrepareType.MakeGenericType(oneType); tempPrepare = Activator.CreateInstance(tempType, new object[] { null, null, tempFilter }) as IAutofacContainerPrepare; } catch (Exception) { tempPrepare = null; } return(tempPrepare); }