当前位置: 首页>>代码示例>>C#>>正文


C# RuleSet.GetEffectiveRules方法代码示例

本文整理汇总了C#中RuleSet.GetEffectiveRules方法的典型用法代码示例。如果您正苦于以下问题:C# RuleSet.GetEffectiveRules方法的具体用法?C# RuleSet.GetEffectiveRules怎么用?C# RuleSet.GetEffectiveRules使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在RuleSet的用法示例。


在下文中一共展示了RuleSet.GetEffectiveRules方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FindConflictsCore

        private RuleConflictInfo FindConflictsCore(RuleSet baselineRuleSet, RuleSet targetRuleSet, params string[] ruleSetDirectories)
        {
            string[] directories = this.ruleSetSearchDirectories
                .Union(ruleSetDirectories)
                .Union(new[]
                {
                    Path.GetDirectoryName(baselineRuleSet.FilePath),
                    Path.GetDirectoryName(targetRuleSet.FilePath)
                }).ToArray();

            // RuleProviders are used in practice for IncludeAll purposes i.e. AllRule.ruleset will be 
            // included with the specified action. In out cases only care about the baseline so we could create
            // a wrapper that provides the baseline rules, but in the context of the problem this is not required
            // since we will be able to find that the rules were missing just by diffing with the results from 
            // GetEffectiveRules, see more details below.
            RuleInfoProvider[] providers = new RuleInfoProvider[0];

            // Underlying implementation details of GetEffectiveRules:
            // The method will return a list of rules, some are the same as specified, and for some the Action will change 
            // according to the merge rules:

            // 1. If there's an <IncludeAll Action="..." /> then all the provider's rules will include all the rules 
            // with the specified Action (can't be None or Default).
            //
            // 2. Then the includes will be take into account (essentially the same flow 1-3, recursively):
            //    a. If the Include Action is None -> will not be included
            //    b. If the Include Action is Default -> rule actions remains as defined
            //    b. otherwise it will get the Action from the Include
            //  Once the Include rule Action is determined it will be merged but could only change to more strict value !
            // 
            // 3. Lastly the file-level rules are added with their Action (overriding any previous settings).
            //
            // To summarize in practical terms: 
            // (●) IncludeAll - will not impact us because all the baseline rules will be merged on top of (1)
            //  and will either be more strict or the IncludeAll action will be more strict 
            //  => either way won't weaken.
            // 
            // (●) By default i.e. the way we generate the targetRuleSet (which we expect it to be the project level ruleset),
            // the includes cannot weaken the base line ruleset (which we expect it to be the solution level ruleset)
            // due to to the include merge rules which prefer strictness and the fact that we use Default as the Include Action.
            // 
            // (●) The only thing that can weaken the rules in practice, in the default case, are ruleset level rules 
            // i.e what you get when you modify the ruleset by using the ruleset editor UI.
            // 
            // (●) In the none default case, when the user decides to manually edit the file, setting the baseline include
            // to None or removing it will use the other Includes' Action settings which could weaken the target ruleset.
            // 
            // With that in mind, we will find "conflicts" of two types - rule with weaker Action or a missing rule.
            // (assuming that all the rules were found with the help of ruleSetDirectories).
            // 
            // In terms of how to fix the conflict...  we could either just add all the conflicted rules into the project. That 
            // will not be optimal in terms of the user or the rebind/update experience. A better approach would be something like:
            // 1. Reset the baseline Include to Action=Default
            // 2. If there are still conflicts, remove all the conflicting rules which are directly on target ruleset.
            // At this point we should not have any conflicts, so there's should not be a need to add the remaining conflicting 
            // rules directly under the target with Action=TheExpectedAction

            var effectiveRulesMap = targetRuleSet.GetEffectiveRules(directories, providers, this.EffectiveRulesErrorHandler)
                .ToDictionary(r => r.FullId, StringComparer.OrdinalIgnoreCase);

            Dictionary<RuleReference, RuleAction> weakenedRules = baselineRuleSet.Rules.Select(r =>
            {
                RuleReference reference;
                if (effectiveRulesMap.TryGetValue(r.FullId, out reference))
                {
                    Debug.Assert(reference.Action != RuleAction.None, "Expected to be found in the missing set");

                    if (IsBaselineWeakend(r.Action, reference.Action))
                    {
                        return new
                        {
                            Rule = reference,
                            ExpectedAction = r.Action
                        };
                    }
                }

                return null;
            }).Where(r => r != null).ToDictionary(r => r.Rule, r => r.ExpectedAction);

            var missingRules = baselineRuleSet.Rules.Where(r => r.Action != RuleAction.None && !effectiveRulesMap.ContainsKey(r.FullId));

            return new RuleConflictInfo(missingRules, weakenedRules);
        }
开发者ID:duncanpMS,项目名称:sonarlint-visualstudio,代码行数:84,代码来源:RuleSetInspector.cs


注:本文中的RuleSet.GetEffectiveRules方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。