如何在自定义FxCop规则中检测自动实现的属性?

时间:2015-06-08 21:07:14

标签: .net fxcop

我正在尝试为DTO编写规则,只允许自动实现的属性。如何使用FxCop API检测到属性是自动实现的?

2 个答案:

答案 0 :(得分:3)

实际上,用户在IL编译代码中实现的自动属性和属性之间存在差异。自动属性设置器和getter使用System.Runtime.CompilerServices.CompilerGeneratedAttribute标记。

Auto properties compiled IL code

因此,使用NDepend工具where you can write custom code queries and custom code rules through C# LINQ queries,匹配自动属性getter和setter只是编写LINQ查询的问题:

from m in Application.Methods
where (m.IsPropertyGetter|| m.IsPropertySetter)
    && m.IsGeneratedByCompiler
select m

NDepend auto-properties code query matching

请注意,在上面的屏幕截图中,您可能会在Resources生成的类中匹配getter和setter,为了防止这种情况,您可以添加&& !m.ParentType.IsUsing("System.Resources.ResourceManager")之类的子句:

NDepend auto-properties code query matching without resources matching

当然,所有这些都可以适应FxCop自定义裁决系统。

Disclamer:我为NDepend工作

答案 1 :(得分:0)

感谢NDepend团队的@Patrick。这是生成的FxCop规则的代码。

using Microsoft.FxCop.Sdk;
using System.Linq;
using System.Runtime.CompilerServices;

namespace CustomFxRules
{
    internal sealed class IsAutoImplementedProperty : BaseIntrospectionRule
    {
        public IsAutoImplementedProperty()
            : base("IsAutoImplementedProperty", "CustomFxRules.CustomFxRules", typeof(IsAutoImplementedProperty).Assembly)
        {
        }

        public override TargetVisibilities TargetVisibility
        {
            get { return TargetVisibilities.ExternallyVisible; }
        }

        private TypeNode CompilerGeneratedAttributeType { get; set; }

        public override void BeforeAnalysis()
        {
            base.BeforeAnalysis();

            this.CompilerGeneratedAttributeType = FrameworkAssemblies.Mscorlib.GetType(
                Identifier.For("System.Reflection"),
                Identifier.For("CompilerGeneratedAttribute"));
        }

        public override ProblemCollection Check(Member member)
        {
            if (member.NodeType != NodeType.Property)
                return base.Check(member);

            var property = (PropertyNode)member;
            VisitProperty(property);
            return this.Problems;
        }

        private const string AddAutoImplementedPropertyResolutionName = "Add auto-implemented property";

        public override void VisitProperty(PropertyNode property)
        {
            if (property.Getter == null)
            {
                Problems.Add(new Problem(new Resolution(AddAutoImplementedPropertyResolutionName, "{0} property must have a getter", property.FullName)));
            }
            else if (property.Getter.Attributes.All(attributeNode => attributeNode.Type.FullName != typeof(CompilerGeneratedAttribute).FullName))
            {
                Problems.Add(new Problem(new Resolution(AddAutoImplementedPropertyResolutionName, "{0} property must have an auto-impelemented getter", property.FullName)));
            }

            if (property.Setter == null)
            {
                Problems.Add(new Problem(new Resolution(AddAutoImplementedPropertyResolutionName, "{0} property must have a setter", property.FullName)));
            }
            else if (property.Setter.Attributes.All(attributeNode => attributeNode.Type.FullName != typeof(CompilerGeneratedAttribute).FullName))
            {
                Problems.Add(new Problem(new Resolution(AddAutoImplementedPropertyResolutionName, "{0} property must have an auto-impelemented setter", property.FullName)));
            }
        }
    }
}