예제 #1
0
        internal AppModuleDescriptor(IAppModule instance)
        {
            ModuleType = instance.GetType();
            Assembly   = ModuleType.Assembly;
            Instance   = instance;

            _dependencies = new List <AppModuleDescriptor>();
        }
 private void FillTypeSet(ref HashSet <Type> set, IAppModule appModule)
 {
     set.Add(appModule.GetType());
     if (appModule.DependentModules == null)
     {
         return;
     }
     foreach (var module in appModule.DependentModules)
     {
         FillTypeSet(ref set, module);
     }
 }
예제 #3
0
        public void TryRegister(IAppModule appModule)
        {
            var scannedMethods = appModule.GetType().GetMethods()
                                 .Where(x => x.ReturnType != typeof(void))
                                 .Select(x => new { Method = x, Attribute = x.GetCustomAttribute <ReplaceCreatorAttribute>() })
                                 .Where(x => x.Attribute != null)
                                 .Select(x => x.Method);

            foreach (var method in scannedMethods)
            {
                this.TryRegister(appModule, method);
            }
        }
        /// <summary>
        /// 从 <see cref="IAppModule"/> 实例的方法中创建组件,
        /// 这些方法必须被标记 <see cref="ComponentAttribute"/> 特征。
        /// </summary>
        /// <param name="targetModule"></param>
        /// <param name="autofacContainerBuilder"></param>
        private static void ReplaceComponentFromMethods(IAppModule targetModule, AutofacContainerBuilder autofacContainerBuilder)
        {
            var methods = targetModule.GetType().GetMethods()
                          .Where(x => x.ReturnType != typeof(void))
                          .Select(x => new { Method = x, Attribute = x.GetCustomAttribute <ReplaceCreatorAttribute>() })
                          .Where(x => x.Attribute != null)
                          .Select(x => x.Method);

            foreach (var method in methods)
            {
                RemoveService(targetModule, autofacContainerBuilder, method);
            }
            //RegisterMethods(targetModule, autofacContainerBuilder, methods);
        }
        public IEnumerable <AttributeAndTypeInfo> Scan(IAppModule appModule)
        {
            if (appModule == null)
            {
                return new AttributeAndTypeInfo[] { }
            }
            ;

            var assembly     = appModule.GetType().Assembly;
            var assemblyName = assembly.FullName;
            IEnumerable <AttributeAndTypeInfo> result;

            if (sacennedAssemlyNameToInfoCache.TryGetValue(assemblyName, out result))
            {
                return(result);
            }

            Type[] types = assembly.GetExportedTypes();
            IList <AttributeAndTypeInfo> scannableAttributeAndTypeInfos
                = new List <AttributeAndTypeInfo>();

            foreach (Type type in types)
            {
                object[] objects = type.GetCustomAttributes(typeof(ScannableAttribute), true);
                if (objects.Length == 0)
                {
                    continue;
                }

                foreach (var obj in objects)
                {
                    AttributeAndTypeInfo attributeAndTypeInfo
                        = new AttributeAndTypeInfo(obj as ScannableAttribute, type);
                    scannableAttributeAndTypeInfos.Add(
                        attributeAndTypeInfo
                        );
                }
            }

            sacennedAssemlyNameToInfoCache[assemblyName] = scannableAttributeAndTypeInfos;
            return(scannableAttributeAndTypeInfos);
        }
    }