/// <summary>
		/// 	creates  assembly manifest definition
		/// </summary>
		public AssemblyManifestDefinition(Assembly assembly, bool needExportAttribute) {
			if (assembly == null) {
				throw new ArgumentNullException("assembly");
			}
			// we use indirect attribute usage to avoid msbuild context problems - we have to make compoents even in different versions of system
			ComponentDefinitions = new List<ManifestClassDefinition>();

			AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

			try {
				var predesc =
					assembly.GetCustomAttributes(true).FirstOrDefault(
						x =>
							{
								var baseType = x.GetType().BaseType;
								return baseType != null && (x.GetType().Name == typeof (ContainerExportAttribute).Name ||
							                                 baseType.Name == typeof (ContainerExportAttribute).Name);
							});
				if (null != predesc) {
					Descriptor = new ContainerExportAttribute
						{
							Priority = predesc.GetValue<int>("Priority"),
							Lifestyle = predesc.GetValue<Lifestyle>("Lifestyle")
						};
				}
			}
			finally {
				AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
			}
			if (null == Descriptor && !needExportAttribute) {
				Descriptor = new ContainerExportAttribute(-1);
			}
			if (null != Descriptor) {
				foreach (var type in assembly.GetTypes()) {
					if (type.IsAbstract) {
						continue; //cannot expose abstracts
					}
				    var attributes = type.GetCustomAttributes(true).Where(x => {
				        var baseType = x.GetType().BaseType;
				        return baseType != null && (x.GetType().Name == typeof (ContainerComponentAttribute).Name
				                                    ||
				                                    baseType.Name == typeof (ContainerComponentAttribute).Name);
				    }).ToArray();
				    foreach (var attribute in attributes) {
                        var clsdef = new ManifestClassDefinition(type,new [] { attribute});
				        if (null != clsdef.Descriptors && 0 != clsdef.Descriptors.Count) {
                            ComponentDefinitions.Add(clsdef);
                            clsdef.AssemblyManifest = this;
                        }    
				    }
					
				}
			}
		}
Example #2
0
        /// <summary>
        ///     Loads all components defined on type
        /// </summary>
        /// <param name="type"> </param>
        /// <returns> </returns>
        public IEnumerable <IComponentDefinition> LoadType(Type type)
        {
            var mcd = ManifestClassDefinition.GetAllClassManifests(type).ToArray();
            IList <IComponentDefinition> components = new List <IComponentDefinition>();

            foreach (var classDefinition in mcd)
            {
                foreach (var component in classDefinition.GetComponents())
                {
                    _container.Register(component);
                    components.Add(component);
                }
            }

            return(components.ToArray());
        }
Example #3
0
        /// <summary>
        ///     creates  assembly manifest definition
        /// </summary>
        public AssemblyManifestDefinition(Assembly assembly, bool needExportAttribute)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }
            // we use indirect attribute usage to avoid msbuild context problems - we have to make compoents even in different versions of system
            ComponentDefinitions = new List <ManifestClassDefinition>();

            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

            try {
                var predesc =
                    assembly.GetCustomAttributes(true).FirstOrDefault(
                        x =>
                {
                    var baseType = x.GetType().BaseType;
                    return(baseType != null && (x.GetType().Name == typeof(ContainerExportAttribute).Name ||
                                                baseType.Name == typeof(ContainerExportAttribute).Name));
                });
                if (null != predesc)
                {
                    Descriptor = new ContainerExportAttribute
                    {
                        Priority  = predesc.GetValue <int>("Priority"),
                        Lifestyle = predesc.GetValue <Lifestyle>("Lifestyle")
                    };
                }
            }
            finally {
                AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
            }
            if (null == Descriptor && !needExportAttribute)
            {
                Descriptor = new ContainerExportAttribute(-1);
            }
            if (null != Descriptor)
            {
                foreach (var type in assembly.GetTypes())
                {
                    if (type.IsAbstract)
                    {
                        continue;                         //cannot expose abstracts
                    }
                    var attributes = type.GetCustomAttributes(true).Where(x => {
                        var baseType = x.GetType().BaseType;
                        return(baseType != null && (x.GetType().Name == typeof(ContainerComponentAttribute).Name
                                                    ||
                                                    baseType.Name == typeof(ContainerComponentAttribute).Name));
                    }).ToArray();
                    foreach (var attribute in attributes)
                    {
                        var clsdef = new ManifestClassDefinition(type, new [] { attribute });
                        if (null != clsdef.Descriptors && 0 != clsdef.Descriptors.Count)
                        {
                            ComponentDefinitions.Add(clsdef);
                            clsdef.AssemblyManifest = this;
                        }
                    }
                }
            }
        }