locked
check only 'Properties' name standerdization leaving other public/private variables in FxCop Custom rule RRS feed

  • Question

  • While creating Custom rule in FxCop how can warnings can be shown only for Properties and not for other Public variable?
    For Example:
    I have a rule saying all Properties name should start with 'Upper case Letter'
    private int m_id = -1;
        public int ID
        {
            get

            {
                return  m_id;

            }

            set

            {

                m_id = value;

            }

        }

        private string m_name = string.Empty;
        public string Name
        {
            get

            {

                return m_name;

            }

            set

            {

                m_name = value;

            }

        }

        private string strLastName = "";

        public string strFirstName = "";


    The above code should not show any warnings. If I have a property

    private string m_sname = string.Empty;
        public string sName
        {
            get
            {
                return m_sname;

            }

            set

            {

                m_sname = value;

            }

        }



    this code should display a warning.





    As we can find which access modifier but how can we differentiate between Property and Public variables
    Thanks
    Wednesday, December 16, 2009 4:32 AM

Answers

  • If you want to check only public members, override the TargetVisibility property of your rule.  e.g.:

    public override TargetVisibilities TargetVisibility
    {
    	get
    	{
    		return TargetVisibilities.ExternallyVisible;
    	}
    }
    If you only want to check properties, then you should only check members that are castable to PropertyNode.  e.g.:

    public override ProblemCollection Check(Member member)
    {
    	PropertyNode propertyNode = member as PropertyNode;
    	if (propertyNode != null)
    	{
    		// Apply your rule logic here.
    	}
    
    	return this.Problems;
    }
    BTW, the Naming.IdentifiersShouldBeCasedCorrectly rule will check that public properties are pascal-cased, but it ignores fields (public or otherwise), so it should already exhibit the behaviour you want.
    • Marked as answer by liurong luo Wednesday, December 23, 2009 5:58 AM
    Wednesday, December 16, 2009 1:32 PM

All replies

  • If you want to check only public members, override the TargetVisibility property of your rule.  e.g.:

    public override TargetVisibilities TargetVisibility
    {
    	get
    	{
    		return TargetVisibilities.ExternallyVisible;
    	}
    }
    If you only want to check properties, then you should only check members that are castable to PropertyNode.  e.g.:

    public override ProblemCollection Check(Member member)
    {
    	PropertyNode propertyNode = member as PropertyNode;
    	if (propertyNode != null)
    	{
    		// Apply your rule logic here.
    	}
    
    	return this.Problems;
    }
    BTW, the Naming.IdentifiersShouldBeCasedCorrectly rule will check that public properties are pascal-cased, but it ignores fields (public or otherwise), so it should already exhibit the behaviour you want.
    • Marked as answer by liurong luo Wednesday, December 23, 2009 5:58 AM
    Wednesday, December 16, 2009 1:32 PM
  • thanks for the help.
    I added my rule logic

    if

     

    (propertyNode.Type == FrameworkTypes.String)

    {

     

    if (!propertyNode.FullName.StartsWith("s"))

    {

     

    this.Problems.Add(new Problem(this.GetResolution(propertyNode.FullName)));

    }

    }

     

    if (propertyNode.Type == FrameworkTypes.Int32)

    {

     

    if (!propertyNode.FullName.StartsWith("i"))

    {

     

    this.Problems.Add(new Problem(this.GetResolution(propertyNode.FullName)));

    }

    }

     

    if (propertyNode.Type == FrameworkTypes.Double)

    {

     

    if (!propertyNode.FullName.StartsWith("d"))

    {

     

    this.Problems.Add(new Problem(this.GetResolution(propertyNode.FullName)));

    }

    }

     

    if (propertyNode.Type == FrameworkTypes.Object)

    {

     

    if (!propertyNode.FullName.StartsWith("o"))

    {

     

    this.Problems.Add(new Problem(this.GetResolution(propertyNode.FullName)));

    }

    }

     

    if (propertyNode.Type == FrameworkTypes.Single)

    {

     

    if (!propertyNode.FullName.StartsWith("f"))

    {

     

    this.Problems.Add(new Problem(this.GetResolution(propertyNode.FullName)));

    }

    }

    }

    I also need to check whether the naming follows CamelCase standard or not. How can I add that rule also?
    Thanks in advance

    Friday, December 18, 2009 3:04 PM
  • Would it be OK for a string property to be named "fuddleDuddle"?  This won't be caught by your rule since you have focused on hungarian-style prefixes rather than on casing.

    Also, what do you mean by "I also need to check whether the naming follows CamelCase standard or not"?  Do you want your property names to be camel-cased or pascal-cased?  The latter is the .NET standard, and there is a built-in FxCop rule that checks for it.
    Friday, December 18, 2009 5:42 PM