/// <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;
                        }    
				    }
					
				}
			}
		}
Esempio n. 2
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;
                        }
                    }
                }
            }
        }