コード例 #1
0
 public TypeFilterTests()
 {
     this.testee = new TypeFilter();
 }
コード例 #2
0
ファイル: TypeCreator.cs プロジェクト: pietervp/MData
        /// <summary>
        /// Registers an assembly. This means scanning an assembly for interfaces with the MData attribute applied.
        /// If such an interface was found in the given assembly, it gets mapped with a generic LogicBase instance,
        /// or a user specified logic class. This logic class is discovered in all loaded assemblies
        /// </summary>
        /// <param name="assembly"></param>
        internal void RegisterAssembly(Assembly assembly)
        {
            //we already scanned this assembly, no need to do it again
            if (_scannedAssemblies.Contains(assembly.GetName().ToString()))
                return;

            //if congifurator was configured with allowedAssemblies, then only scan assemblies of that collection
            if (_configurator.Assemblies != null && !_configurator.Assemblies.Any(x => x.GetName().ToString() == assembly.GetName().ToString()))
                return;

            //add the given assembly as scanned to avoid stackoverflows (GetLogicClass could do a call to this method)
            _scannedAssemblies.Add(assembly.GetName().ToString());

            //initialize variables to filter the assemblies types
            var typeFilter = new TypeFilter();
            var types = assembly.GetTypes();

            //loop all types of the assembly decortated with the MData attribute
            foreach (var domainType in types.Where(x => typeFilter.HasAttribute(x, typeof (MDataAttribute))))
            {
                //if the type was already registered, do no bother further processing
                if (_domainToLogic.ContainsKey(domainType))
                    continue;

                //discover the logic class (either userdefined or generic)
                var logicType = GetLogicClass(domainType, types, typeFilter) ?? GetLogicClass(domainType);

                //we should always get a type of the logic class, if not throw an error
                if (logicType == null)
                    throw new InvalidOperationException(string.Format("Could not find a userdefined/generic LogicBase descendant for type {0}", domainType.GetName()));

                //all ok, add the 'domain to logic' mapping
                _domainToLogic.Add(domainType, logicType);
            }
        }