示例#1
0
        /// <summary>
        ///     Sort the protection according to their dependency.
        /// </summary>
        /// <returns>Sorted protections with respect to dependencies.</returns>
        /// <exception cref="T:CircularDependencyException">
        ///     The protections contain circular dependencies.
        /// </exception>
        public IList <Protection> SortDependency()
        {
            /* Here we do a topological sort of the protections.
             * First we construct a dependency graph of the protections.
             * The edges in the graph is recorded in a list.
             * Then the graph is sorted starting from the null root node.
             */

            var edges = new List <DependencyGraphEdge>();
            var roots = new HashSet <Protection>(protections);
            Dictionary <string, Protection> id2prot = protections.ToDictionary(prot => prot.FullId, prot => prot);

            foreach (Protection prot in protections)
            {
                Type protType = prot.GetType();

                BeforeProtectionAttribute before = protType
                                                   .GetCustomAttributes(typeof(BeforeProtectionAttribute), false)
                                                   .Cast <BeforeProtectionAttribute>()
                                                   .SingleOrDefault();
                if (before != null)
                {
                    // current -> target
                    IEnumerable <Protection> targets = before.Ids.Select(id => id2prot[id]);
                    foreach (Protection target in targets)
                    {
                        edges.Add(new DependencyGraphEdge(prot, target));
                        roots.Remove(target);
                    }
                }

                AfterProtectionAttribute after = protType
                                                 .GetCustomAttributes(typeof(AfterProtectionAttribute), false)
                                                 .Cast <AfterProtectionAttribute>()
                                                 .SingleOrDefault();
                if (after != null)
                {
                    // target -> current
                    IEnumerable <Protection> targets = after.Ids.Select(id => id2prot[id]);
                    foreach (Protection target in targets)
                    {
                        edges.Add(new DependencyGraphEdge(target, prot));
                        roots.Remove(prot);
                    }
                }
            }

            IEnumerable <Protection> sorted = SortGraph(roots, edges);

            return(sorted.ToList());
        }
示例#2
0
        /// <summary>
        ///     Sort the protection according to their dependency.
        /// </summary>
        /// <returns>Sorted protections with respect to dependencies.</returns>
        /// <exception cref="T:CircularDependencyException">
        ///     The protections contain circular dependencies.
        /// </exception>
        // Token: 0x06000139 RID: 313 RVA: 0x0000A85C File Offset: 0x00008A5C
        public IList <Protection> SortDependency()
        {
            List <DependencyResolver.DependencyGraphEdge> edges = new List <DependencyResolver.DependencyGraphEdge>();
            HashSet <Protection>            roots   = new HashSet <Protection>(this.protections);
            Dictionary <string, Protection> id2prot = this.protections.ToDictionary((Protection prot) => prot.FullId, (Protection prot) => prot);

            foreach (Protection prot2 in this.protections)
            {
                Type protType = prot2.GetType();
                BeforeProtectionAttribute before = protType.GetCustomAttributes(typeof(BeforeProtectionAttribute), false).Cast <BeforeProtectionAttribute>().SingleOrDefault <BeforeProtectionAttribute>();
                if (before != null)
                {
                    IEnumerable <Protection> targets = from id in before.Ids
                                                       select id2prot[id];
                    foreach (Protection target in targets)
                    {
                        edges.Add(new DependencyResolver.DependencyGraphEdge(prot2, target));
                        roots.Remove(target);
                    }
                }
                AfterProtectionAttribute after = protType.GetCustomAttributes(typeof(AfterProtectionAttribute), false).Cast <AfterProtectionAttribute>().SingleOrDefault <AfterProtectionAttribute>();
                if (after != null)
                {
                    IEnumerable <Protection> targets2 = from id in after.Ids
                                                        select id2prot[id];
                    foreach (Protection target2 in targets2)
                    {
                        edges.Add(new DependencyResolver.DependencyGraphEdge(target2, prot2));
                        roots.Remove(prot2);
                    }
                }
            }
            IEnumerable <Protection> sorted = this.SortGraph(roots, edges);

            return(sorted.ToList <Protection>());
        }