示例#1
0
        /// <summary>
        ///     Configures assembly to container if ContainerExport attribute defined
        /// </summary>
        /// <param name="assembly"> </param>
        /// <param name="requireManifest"> </param>
        /// <param name="context"></param>
        /// <returns> </returns>
        public IEnumerable <IComponentDefinition> LoadAssembly(Assembly assembly, bool requireManifest = false, object context = null, bool autoonly = false)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }

            var am     = new AssemblyManifestDefinition(assembly, needExportAttribute: false);
            var result = new List <IComponentDefinition>();

            if (null != am.Descriptor || !requireManifest)
            {
                foreach (var definition in am.ComponentDefinitions)
                {
                    foreach (var componentDefinition in definition.GetComponents())
                    {
                        _container.Register(componentDefinition);
                        result.Add(componentDefinition);
                    }
                }
            }
            //if assembly setupers exists - they will be called instead of auto-configuration
            var initializers =
                assembly.GetTypes().Where(_ => null != _.GetCustomAttribute <ContainerInitializerAttribute>()).ToArray();

            if (0 != initializers.Length)
            {
                result = result.Union(initializers.Select(_ => _.Create <IContainerInitializer>())
                                      .SelectMany(_ => _.Setup(_container, context))).ToList();
            }
            return(result.ToArray());
        }
		/// <summary>
		/// 	generates single bxl string from assembly manifests
		/// </summary>
		/// <param name="assemblies"> </param>
		/// <param name="options"> </param>
		/// <returns> </returns>
		public string GenerateManifest(Assembly[] assemblies, ManifestBxlGeneratorOptions options = null) {
			if (assemblies == null) {
				throw new ArgumentNullException("assemblies");
			}
			if (0 == assemblies.Length) {
				throw new ArgumentException("no assemblies given");
			}
			var gen = new BxlGenerator();
			var genopts = new BxlGeneratorOptions
				{
					NoRootElement = true,
					UseTrippleQuotOnValues = false,
					InlineAlwaysAttributes = new[] {"code", "name", "priority"}
				};
			options = options ?? new ManifestBxlGeneratorOptions();
			var xml = new XElement("root");
			IList<IComponentDefinition> componentsToGenerate = new List<IComponentDefinition>();
			foreach (var assembly in assemblies.Distinct()) {
				Console.WriteLine("Enter: " + assembly.GetName().Name);
				var def = new AssemblyManifestDefinition(assembly, NeedExportAttribute);

				if (def.Descriptor != null) {
					Console.WriteLine("IsConfigurable: " + assembly.GetName().Name);
					foreach (var classDefinition in def.ComponentDefinitions) {
						componentsToGenerate.Add(classDefinition.GetComponent());
					}
				}
			}
			var useshortnames = false;
			if (options.UseShortNames) {
				useshortnames = CanUseShortNames(componentsToGenerate);
			}
			if (useshortnames) {
				foreach (var assembly in assemblies) {
					var def = new AssemblyManifestDefinition(assembly, NeedExportAttribute);
					if (def.Descriptor != null) {
						xml.Add(new XElement("ref", new XAttribute("code", assembly.GetName().Name)));
					}
				}
				var namespaces =
					componentsToGenerate.Select(x => x.ServiceType).Union(componentsToGenerate.Select(x => x.ImplementationType))
						.Distinct().Select(x => x.Namespace).Distinct();
				foreach (var ns in namespaces) {
					xml.Add(new XElement("using", new XAttribute("code", ns)));
				}
			}
			foreach (var definition in componentsToGenerate) {
				var elementname = definition.Lifestyle.ToString().ToLowerInvariant();
				var value = GetTypeName(definition.ServiceType, useshortnames);
				var e = new XElement(elementname, value);
				if (definition.Name.IsEmpty()) {
					e.Add(new XAttribute("code", GetTypeName(definition.ImplementationType, useshortnames)));
				}
				else {
					e.Add(new XAttribute("code", definition.Name));
					e.Add(new XAttribute("name", GetTypeName(definition.ImplementationType, useshortnames)));
				}
				e.Add(new XAttribute("priority", definition.Priority));
				if (definition.Role.IsNotEmpty()) {
					e.Add(new XAttribute("role", definition.Role));
				}
				if (definition.Help.IsNotEmpty()) {
					e.Add(new XAttribute("help", definition.Help));
				}
				xml.Add(e);
			}

			var result = gen.Convert(xml, genopts);
			return result;
		}