StyleCop SDK
Created by:
Microsoft
Corporation
Version 3.1.5
StyleCop was developed to facilitate maintaining a consistent C# coding style across the company. Historically, different product groups at Microsoft have worked with drastically different coding styles. Some groups have even used varying code styles within a single product, sometimes even in the same code module.
With the advent and adoption of the C# programming language at Microsoft, many teams have seen an opportunity to enforce a consistent set of coding guidelines across the company. StyleCop was written to make it easier for developers to adhere to these guidelines, and to provide a simple way to analyze a code base for style uniformity.
StyleCop provides an extensibility model to allow developers to add new rules, which can be plugged in and run alongside the default rules shipped with StyleCop.
Creating a project:
To create a StyleCop rules add-in,
create a new class library project in
NOTE: Because Whidbey has not
shipped yet, there are multiple versions of
Once the project has been created and StyleCopCore has been referenced, create a class within the project and inherit the class from StyleCopAddin. This class contains various abstract methods and properties which must be overridden in your derived class. This is described in more detail in the StyleCopAddin section of this document.
Installation:
Once the add-in has been built, there are three ways to install the add-in so that it will be loaded by StyleCop. First, the add-in may be placed underneath StyleCop’s installation path. StyleCop will automatically discover and load add-ins under its installation path.
Secondly, additional add-in
discovery paths can be added to the system registry. StyleCop will parse each
of these paths and load any add-ins found under the path or any of its
sub-directories. Add-in search paths for StyleCop 2003 should be added under
the following key: HKCU\Software\Microsoft\StyleCop2003\Addins. Add-in search paths for StyleCop 2005 (VS
PD5) should be added under HKCU\Software\Microsoft\StyleCop2005PD5\Addins. Search paths for StyleCop 2005 (VS Beta1)
should be added under HKCU\Software\Microsoft\StyleCop2005Beta1\Addins. (NOTE: This path will change to ...\StyleCop2005\Addins when the final version of Whidbey is
released).
To add a search path to the registry, choose the correct registry path
and add a new string value under the StyleCopXXX\Addins key. The name of the
value can be any unique string. The value data must specify the full path to
the add-in search path.
The StyleCopCmd tool provides a third way to specify additional add-in
search paths. Search paths can be specified on the command line using the –p
flag. See the StyleCop User Guide document for more information.
Specifying violation types:
StyleCop rules add-ins must register a list of violation types that are
exported from the add-in. The add-in will reference these violation types when
registering a rules violation found in a C# code file. The violation types list
is provided in the form of an Xml document. When the add-in is loaded, StyleCop
will call the add-in’s AddinXml property. The add-in must hand
back a fully-formed Xml document containing the complete list of violation
types that can be discovered by the add-in. The format of the Xml file is as
follows:
<stylecopaddin id={unique
add-in id} name={friendly name}
<violationtype id={unique
violation type id} name={friendly name for the violation type}>
<context>{context
message string for the violation type}</context>
</violationtype>
</stylecopaddin>
The {unique add-in id} field must be filled in with a unique
guid that identifies the add-in. The {friendly name} field must provide a short
name for the add-in. This name will appear in the StyleCop Settings dialog to
identify the add-in. Each violation type must also be given a unique guid and
friendly name. Finally, each violation type must contain a context node which provides a context message for the violation
type. When a violation on this type is discovered in a code file, this message
will be presented to the developer to explain the violation.
For example, the following violation types list
file is exported by the “File Headers” add-in that ships with StyleCop:
<?xml
version="1.0" encoding="utf-8" ?>
<stylecopaddin
id="31B0AB2A-8EED-4815-9F2D-C5A439EA9809" name="File
headers">
<violationtype
id="C4E60EC4-A3FB-4D1E-92EC-48A5A2F0A81D"
name="FileHeaderMissing">
<context>The
file has no header, the header Xml is invalid, or the header is not at the top
of the file.</context>
</violationtype>
<violationtype
id="29BA77C5-B9DB-4EEA-AAC1-9B864AC9D5AB"
name="FileHeaderCopyrightMissing">
<context>The
'copyright' tag is missing from the file header.</context>
</violationtype>
<violationtype
id="0386A13B-DBB2-47E3-AAA1-397AA6D1B715"
name="FileHeaderCopyrightEmpty">
<context>The
'copyright' tag in the file header is empty.</context>
</violationtype>
<violationtype
id="FE78735E-4DA4-44C8-935B-01AD2F3D38BD"
name="FileHeaderCopyrightFileMissing">
<context>The
'file' attribute is missing from the file header 'copyright'
tag.</context>
</violationtype>
<violationtype
id="C5BD564A-6DF8-430E-80C2-D1E5CD86B227"
name="FileHeaderFileNameMismatch">
<context>The
'file' attribute in the file header's 'copyright' tag does not contain the name
of the file.</context>
</violationtype>
<violationtype
id="6A3A7A40-C91B-40C5-B159-FDF855D1483D"
name="FileHeaderSummaryMissing">
<context>The
'summary' tag is missing from the file header, or it is empty.</context>
</violationtype>
<violationtype
id="A2AD697C-C44E-4613-9AED-5E095B67B346"
name="FileHeaderCompanyTagInvalid">
<context>The
'company' attribute in the file header's 'copyright' tag is missing or
empty.</context>
</violationtype>
</stylecopaddin>
A violation type’s context string
may contain in-place string parameters that can be filled in with specific
information when a violation is discovered. For example, consider the following
violation type context message:
The {0} does not
have an access modifier.
This can be filled in later to provide the
type of element that is missing its access modifier. For more information, see
the StyleCopAddin.AddViolation
method.
Every StyleCop add-in class must inherit from the StyleCopAddin class.
System.Object
Microsoft.Tools.StyleCop.StyleCopAddin
Members
Public Properties |
Description |
Returns the unique ID of the add-in. |
|
Gets the name of the add-in. |
|
Protected Abstract
Properties |
Description |
Gets the add-in’s violation type list document. |
|
Public Abstract
Methods |
Description |
Analyze one code file. |
|
Public Virtual
Methods |
Description |
Allows the add-in to place pages on the settings dialog. |
|
Signals that the user has opened a new |
|
Signals that the user has closed the current |
|
Public Methods |
Description |
Adds one violation against a code file. |
Gets the ID of the add-in. Every StyleCop add-in must have its own unique GUID string.
public string Id { get; }
Property Value
A string that contains the unique ID of the add-in.
Gets the name of the add-in.
public string Name { get; }
Property Value
A string that contains the name of the add-in.
Remarks
The name will be presented to the user on the enable/disable rules settings page.
Gets the add-in’s violation type list document. Every StyleCop add-in must override this property.
public string Id { get; }
Property Value
An XmlDocument containing the violation type list for the add-in.
This method is called by the StyleCop core module when it is time for the add-in to run its analyze routines on a particular code file. Every StyleCop add-in must override this method.
public void ProcessFile(CodeFile
file);
Parameters
file
The CodeFile object that represents the file to process.
Remarks
The add-in should call the StyleCopAddin.AddViolation method to report a rules violation in the code file.
This method allows the add-in to provide one or more property pages to display on the StyleCop settings dialog.
public IPropertyControlPage[] GetSettingsPages();
Return Value
Return an array of Control objects implementing IPropertyControlPage, or null if the add-in has no property pages to display.
This method is called when the user opens a new
public void SolutionOpened();
This method is called when the user closes the current
public void SolutionOpened();
This method allows the add-in to provide one or more property pages to display on the StyleCop settings dialog.
public void AddViolation(Element element, string violationId, params object[]
values);
Parameters
element
The code Element that contains the violation.
violationId
The ID of the violation type.
values
Optional array of string values to insert into the violation message text.
This method allows the add-in to provide one or more property pages to display on the StyleCop settings dialog.
public void AddViolation(Element element, int line, string violationId, params
object[] values);
Parameters
element
The code Element that contains the violation.
line
The line in the file on which the violation appears.
violationId
The ID of the violation type.
values
Optional array of string values to insert into the violation message text.
Describes one code element within a file.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.ClassBase
Microsoft.Tools.StyleCop.Class
Microsoft.Tools.StyleCop.Interface
Microsoft.Tools.StyleCop.Struct
Microsoft.Tools.StyleCop.CodeFile
Microsoft.Tools.StyleCop.ScopedElement
Microsoft.Tools.StyleCop.Constructor.ClassInitializer
Microsoft.Tools.StyleCop.Enum.Item
Microsoft.Tools.StyleCop.MethodBase
Microsoft.Tools.StyleCop.Accessor
Microsoft.Tools.StyleCop.Constructor
Microsoft.Tools.StyleCop.Delegate
Microsoft.Tools.StyleCop.Event
Microsoft.Tools.StyleCop.Indexer
Microsoft.Tools.StyleCop.Method
Microsoft.Tools.StyleCop.Property
Microsoft.Tools.StyleCop.MemberVariable
Microsoft.Tools.StyleCop.Namespace
Microsoft.Tools.StyleCop.Using
Members
Public Types |
Description |
Type |
Describes the various types of elements. |
Public Properties |
Description |
The element’s access modifier. |
|
The actual visibility of the element, taking into account the access of the item’s parents all the way up the stack. |
|
A tag that can be used by the add-in analyzer while analyzing the file. |
|
An array of Element objects representing the child elements of this element. |
|
Describes the code within the element. |
|
Describes the element declaration. |
|
The Element.Type of this element. |
|
The fully qualified name of the element, from the namespace down. |
|
The header for the element described by this class. |
|
The line number that the item begins on within the file. |
|
The name of the element described by this class. |
|
The parent element that this element is contained under. |
Enum that describes the various types of code elements processed by StyleCop and its add-ins.
Members
Values |
Description |
Accessor |
An accessor inside of a property or an event. |
Class |
A class. |
Constructor |
A constructor. |
Delegate |
A delegate. |
Destructor |
A destructor. |
Enum |
An enum. |
EnumItem |
An item in an enum element. |
Event |
An event declaration. |
File |
A code file. |
Indexer |
An array indexer into a class. |
Inner |
The inner code from a code scope. |
Interface |
An interface. |
MemberVariable |
A member variable. |
Method |
A method. |
Namespace |
A namespace. |
Property |
A property. |
Struct |
A struct. |
Unknown |
An unknown element. This usually indicates a syntax error in the code being analyzed. |
Using |
A using statement. |
Gets the element’s access modifier type.
public AccessModifierType AccessModifier { get; }
Property Value
The AccessModifierType of the element.
Gets the actual access of the element as viewed outside of the class. This takes into account the access of the item’s parents.
public AccessModifierType ActualAccess { get; }
Property Value
The access of the element as viewed outside of the class.
Gets or sets a tag that can be used by the add-in while analyzing the file. The contents of this tag are reset to null before each add-in is called.
public object AnalyzerTag { get;
set; }
Property Value
An object tag that can be used by the add-in to temporarily store data about the element while analyzing the file.
Gets the collection of child elements that this element contains. The objects in the collection all derive from the Element class.
public ICollection Children { get;
}
Property Value
An ICollection object containing the children of this element.
Gets a value describing the code contained within the element.
public CodeView
CodeView { get; }
Property Value
A CodeView object describing the code contained within the element.
Gets a value describing the element’s declaration code.
public Declaration
Declaration { get; }
Property Value
A Declaration object describing the element’s declaration code.
Gets a value describing the type of the element.
public Element.Type
ElementType { get; }
Property Value
An Element.Type value describing the type of the element.
Gets the fully-qualified namespace name of the element.
public string FullNamespaceName {
get; }
Property Value
A string containing the fully-qualified namespace name of the element.
Gets the Xml header code from above the element, or null if there is no element header for this element.
public string Header { get; }
Property Value
A string value containing the Xml element header.
Gets the line number that the element appears on in the code.
public int LineNumber { get; }
Property Value
An integer containing the line number that the element starts on in the code.
Gets the name of the element that this class describes
public bool Name { get; }
Property Value
A string value containing the name of the element.
Gets the parent element that contains this element, or null if this element describes the root file.
public Element
Parent { get; }
Property Value
A Element object describing the parent of this element.
A base class for all elements types that resemble classes, including classes, structs, and interfaces.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.ClassBase
Microsoft.Tools.StyleCop.Class
Microsoft.Tools.StyleCop.Interface
Microsoft.Tools.StyleCop.Struct
Members
Public Properties |
Description |
The element’s access modifier. |
|
The actual visibility of the element, taking into account the access of the item’s parents all the way up the stack. |
|
Public Methods |
Description |
Indicates whether the given type is used by a property in this class. |
Gets the ClassBase object representing the class’s base class. This returns null if the item has no base class or if the base class is not accessible to StyleCop.
public ClassBase BaseClass { get; }
Property Value
The base class of this item.
Gets the name of the item’s base class, or null if the item has no base class.
public string BaseClassName { get;
}
Property Value
The name of the item’s base class.
Indicates whether the class contains a property that returns a value of the given type.
public bool IsPropertyType(string
type);
Parameters
type
The property type to search for.
Return Value
Returns true if the class contains a property with a return value of the given type.
Describes a code class.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.ClassBase
Microsoft.Tools.StyleCop.Class
Members
Public Properties |
Description |
Indicates whether this is an abstract class. |
|
Indicates whether this is a partial class. |
|
Indicates whether this is a sealed class. |
|
Public Static
Methods |
Description |
Indicates whether the given class contains a member with the given name. |
Gets a value indicating whether the class is marked abstract.
public bool Abstract { get; }
Property Value
Indicates whether the class is abstract.
Gets a value indicating whether the class is marked partial.
public bool Partial { get; }
Property Value
Indicates whether this is a partial class.
Gets a value indicating whether the class is sealed.
public bool Sealed { get; }
Property Value
Indicates whether the class is sealed.
A static method that indicates whether the given class contains a member with the given name.
Parameters
name
The name of the member to search for in the class.
parentClass
The class to search for an element with the given name.
Return Value
Returns a Element object representing the class member matching the given name if found, or null if the class does not contain a member with the given name.
Describes an interface in the code.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.ClassBase
Microsoft.Tools.StyleCop.Interface
Members
This class has no publicly visible members. See the ClassBase class for more information.
Describes a code struct.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.ClassBase
Microsoft.Tools.StyleCop.Struct
Members
This class contains no publicly visible members. See the ClassBase class for more information.
Describes one code file to analyze for rules violations.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.CodeFile
Members
Public Properties |
Description |
Determines whether the code file has been loaded from cache or processed from scratch. |
|
A string containing the complete contents of the file. |
|
The Element contents of the file. |
|
The name of the file. |
|
The path to the file. |
|
Public Methods |
Description |
Gets the document object representing this code file. |
StyleCop caches information about analyzed code files. If the user runs StyleCop on the file again without changing the file in any way since the last analyze run, StyleCop loads the file information, including all violations within the file, from the cache rather than analyzing the file again. This value indicates whether the file was loaded from the cache or whether it has been loaded from scratch.
public bool Cache { get; }
Property Value
A boolean value that determines whether the file was loaded from cache or whether it was processed from scratch.
Gets a value containing the contents of the file in a string.
public string CodeString { get; }
Property Value
A string containing the contents of the file.
Gets a Element object containing information about the contents of the file. This object is the head of a tree of Element objects representing the contents of the file.
public Element
Contents { get; }
Property Value
A Element object containing information about the contents of the file.
Gets the name of the file.
public string FileName { get; }
Property Value
A string containing the name of the file.
Gets the path to the file.
public string Path { get; }
Property Value
A string containing the path to the file.
Gets the document object representing this code file.
public CodeDocument
GetCodeDocument();
Return Value
Returns an object derived from CodeDocument, representing the code file.
Describes an enum element.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.Enum
Members
Public Properties |
Description |
Gets the base type of the enum. |
|
Gets the collection of items in the enum. |
|
Public Methods |
Description |
Gets the enum item at the given index. |
Gets the base type of the enum, or null if there is no base type.
public string BaseType { get; }
Property Value
A string containing the name of the base type for the enum.
Gets the collection of items in the enum. Each item is presented by an Enum.Item class object.
public ICollection Items { get; }
Property Value
The collection of Enum.Item objects representing the items in the enum.
Gets the enum item at the given index.
public Enum.Item
GetItem(int index);
Parameters
index
The index of the enum item to retrieve.
Return Value
Returns an Enum.Item representing the item at the given index.
The base class for all element types that contain executable code. For example, methods, member variables, property accessors, enum items, etc.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.ScopedElement
Microsoft.Tools.StyleCop.Constructor.ClassInitializer
Microsoft.Tools.StyleCop.Enum.Item
Microsoft.Tools.StyleCop.MethodBase
Microsoft.Tools.StyleCop.Accessor
Microsoft.Tools.StyleCop.Constructor
Microsoft.Tools.StyleCop.Delegate
Microsoft.Tools.StyleCop.Event
Microsoft.Tools.StyleCop.Indexer
Microsoft.Tools.StyleCop.Method
Microsoft.Tools.StyleCop.Property
Microsoft.Tools.StyleCop.MemberVariable
Members
Public Properties |
Description |
Body |
A hierarchical tree describing the code contents. |
Gets the head of a tree of Scope objects that describe the code contents.
public Scope
Body { get; }
Property Value
A Scope object describing the code contents.
Represents the initialization code on a constructor.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.ScopedElement
Microsoft.Tools.StyleCop.Constructor.ClassInitializer
Members
This class has no publicly visible members. See the ScopedElement class for more information.
Represents one item within an enum.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.ScopedElement
Microsoft.Tools.StyleCop.Enum.Item
Members
This class has no publicly visible members. See the ScopedElement class for more information.
The base class for all element types that contain executable code. For example, methods, member variables, property accessors, enum items, etc.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.ScopedElement
Microsoft.Tools.StyleCop.MethodBase
Microsoft.Tools.StyleCop.Accessor
Microsoft.Tools.StyleCop.Constructor
Microsoft.Tools.StyleCop.Delegate
Microsoft.Tools.StyleCop.Event
Microsoft.Tools.StyleCop.Indexer
Microsoft.Tools.StyleCop.Method
Microsoft.Tools.StyleCop.Property
Members
Public Properties |
Description |
The type of the value returned from this element. |
|
The collection of parameters passed into the element. |
|
Public Methods |
Description |
Gets the parameter at the given index. |
Gets the type of the value returned from this element, or null if the element does not have a return value.
public string ReturnType { get; }
Property Value
The type of the value returned from this element.
Gets the collection of parameters passed into this element. Each parameter is represented by a Parameter object.
public ICollection Parameters {
get; }
Property Value
The collection of Parameter objects representing the parameters passed into this element.
Gets the parameter at the given index.
public Parameter
GetParameter(int index);
Parameters
index
The index of the parameter to retrieve.
Return Value
Returns the Parameter at the given index.
Describes one accessor within a property, indexer, or event.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.ScopedElement
Microsoft.Tools.StyleCop.MethodBase
Microsoft.Tools.StyleCop.Accessor
Members
Public Types |
Description |
An enum listing the various types of accessors. |
|
Public Properties |
Description |
The type of this accessor. |
An enum listing the possible accessor types.
Members
Values |
Description |
Get |
A get accessor on a property or indexer. |
Set |
A set accessor on a property or indexer. |
Add |
An add accessor on an event. |
Remove |
A remove accessor on an event. |
Gets the type of the accessor.
public Accessor.Type
AccessorType { get; }
Property Value
The type of the accessor.
Describes a
constructor or destructor.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.ScopedElement
Microsoft.Tools.StyleCop.MethodBase
Microsoft.Tools.StyleCop.Constructor
Members
Public Properties |
Description |
Gets the class initializer code for the constructor. |
Gets the initialization code for the constructor.
public Constructor.ClassInitializer
Initializer { get; }
Property Value
A ClassInitializer object representing the initialization code for the constructor.
Describes a
delegate element.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.ScopedElement
Microsoft.Tools.StyleCop.MethodBase
Microsoft.Tools.StyleCop.Delegate
Members
This class has no publicly visible members. See the MethodBase class for more information.
Describes an event element.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.ScopedElement
Microsoft.Tools.StyleCop.MethodBase
Microsoft.Tools.StyleCop.Event
Members
Public Properties |
Description |
Gets the name of the delegate that is used for firing the event. |
Gets the name of the delegate that is used for firing the event.
public string EventDelegate { get;
}
Property Value
The name of the delegate that is used for firing the event.
Describes a class indexer element.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.ScopedElement
Microsoft.Tools.StyleCop.MethodBase
Microsoft.Tools.StyleCop.Indexer
Members
This class has no publicly visible members. See the MethodBase class for more information.
Describes a method element.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.ScopedElement
Microsoft.Tools.StyleCop.MethodBase
Microsoft.Tools.StyleCop.Method
Members
This class has no publicly visible members. See the MethodBase class for more information.
Describes a property element.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.ScopedElement
Microsoft.Tools.StyleCop.MethodBase
Microsoft.Tools.StyleCop.Property
Members
This class has no publicly visible members. See the MethodBase class for more information.
Describes a member variable or field within a class.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.ScopedElement
Microsoft.Tools.StyleCop.MemberVariable
Members
Public Properties |
Description |
Indicates whether the field is marked const. |
|
Gets the initialization code that appears after the equals sign in the variable declaration. |
|
Indicates whether the field is marked readonly. |
|
Gets the type of the field. |
Gets a value indicating whether the field is marked const.
public bool Const { get; }
Property Value
True if the field is const, false if it is not.
Gets the initialization code that appears after the equals sign in the variable declaration, if any.
public WordList
EqualsCode { get; }
Property Value
The initialization code for the field.
Gets a value indicating whether the field is marked readonly.
public bool Readonly { get; }
Property Value
True if the field is readonly, false if it is not.
Gets the type of the field.
public string VariableType { get; }
Property Value
A string containing the type of the field.
Describes a namespace element.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.Namespace
Members
This class has no publicly visible members. See the Element class for more information.
Describes a using element.
System.Object
Microsoft.Tools.StyleCop.Element
Microsoft.Tools.StyleCop.Namespace
Members
This class has no publicly visible members. See the Element class for more information.
Represents a view of the code within an element.
System.Object
Microsoft.Tools.StyleCop.CodeView
Members
Public Properties |
Description |
Indicates whether the code was marked as generated. |
|
The line number that the code begins on. |
|
The list of words that make up the code. |
Gets a value indicating whether the code was marked as generated.
public bool Generated { get; }
Property Value
True if the code was generated, false if not.
Gets the line number that the code begins on in the file.
public int LineNumber { get; }
Property Value
The line number that the code begins on.
Gets the collection of words that make up the code view.
public WordList
Words { get; }
Property Value
The collection of words that make up the code.
The declaration class describes an element declaration.
System.Object
Microsoft.Tools.StyleCop.Declaration
Members
Public Properties |
Description |
Indicates whether the element has an access modifier defined. |
|
The access level of the element. |
|
The type of the element. |
|
The name of the element. |
|
Indicates whether the element has an unknown word in its declaration. |
|
Indicates whether the element is marked static. |
|
Indicates whether the element is marked unsafe. |
|
The list of words that make up the declaration. |
Gets a value indicating whether the item has an access modifier explicitly defined.
public bool AccessModifier { get; }
Property Value
Indicates whether the item has an access modifier defined.
Gets the element access modifier type.
public AccessModifierType AccessModifierType { get; }
Property Value
An AccessModiferType value indicating the access level of the element.
Gets the type of the element.
public Element.Type
ElementType { get; }
Property Value
An Element.Type value indicating the type of the element.
Gets a value indicating whether the item has a name defined.
public bool Name { get; }
Property Value
A boolean value indicating whether the item has a name.
Gets a value indicating whether the item has an unknown word in its declaration.
public bool Other { get; }
Property Value
Indicates whether the item has an unknown word in its declaration.
Gets a value indicating whether the item is marked static.
public bool Static { get; }
Property Value
Indicates whether the item is marked static.
Gets a value indicating whether the item is marked unsafe.
public bool Unsafe { get; }
Property Value
Indicates whether the item is marked unsafe.
Gets the collection of words that make up the declaration.
public WordList
Words { get; }
Property Value
A WordList object containing the list of words that make up the declaration.
Describes one parameter in a method or constructor declaration.
System.Object.
Microsoft.Tools.StyleCop.Parameter
Members
Public Properties |
Description |
The attribute applied to the parameter. |
|
The line number that the parameter appears on. |
|
The name of the parameter. |
|
The type of the parameter. |
Gets the attribute applied to the parameter, if any. For example, out, ref, params, etc.
public string Attribute { get; }
Property Value
The attribute applied to the parameter.
Gets the line number that the parameter appears on in the code file.
public int LineNumber { get; }
Property Value
The line number that the parameter appears on in the code file.
Gets the name of the parameter.
public string Name { get; }
Property Value
The name of the parameter.
Gets the type of the parameter.
public string Type { get; }
Property Value
The type of the parameter.
Describes a scope of code within a larger code block.
System.Object.
Microsoft.Tools.StyleCop.Scope
Members
Public Properties |
Description |
The children of this scope. |
|
The index of this scope in its parent scope’s child list. |
|
The collection of parameters and variables exposed to this scope. |
|
The parent of this scope object. |
|
Public Methods |
Description |
Gets the child object at the given index. |
Gets the collection of children under this scope. The child list consists of Word and Scope objects.
public ICollection Children { get;
}
Property Value
The collection of children under this scope.
Gets the index of this scope in its parent scope’s child list.
public int Index { get; }
Property Value
The index of this scope in its parent scope’s child list.
Gets the list of parameters and variables visible to this scope. The list consists of Parameter objects.
public ICollection Parameters {
get; }
Property Value
The list of parameters visible to this scope.
Gets the parent of this scope. The parent will either be another Scope object or an object deriving from ScopedElement.
public object Parent { get; }
Property Value
The parent of this scope.
Gets the child object at the given index.
public object GetChild(int index);
Parameters
index
The index of the child object to retrieve.
Return Value
Returns the object at the given index. This will either be a Word object or a Scope object.
Describes one word in the code.
System.Object
Microsoft.Tools.StyleCop.Word
Microsoft.Tools.StyleCop.AnonymousParameter
Microsoft.Tools.StyleCop.BracketWord
Microsoft.Tools.StyleCop.WhiteSpace
Members
Public Types |
Description |
The various possible word types. |
|
Public Properties |
Description |
The block type that the word appears in. |
|
The line number in the code file where this word ends. |
|
Indicates whether the word appears in a block of generated code. |
|
The line number in the code file on which the word appears. |
|
The word text. |
|
The type of the word. |
|
Public Methods |
Description |
Returns the word text. |
An enum listing the possible accessor types.
Members
Values |
Description |
Add |
The keyword add |
AddressOf |
An address-of symbol: & |
Arithmetic |
An arithmetic symbol: +, -, *, /, etc. |
As |
The keyword as |
AssemblyTag |
An assembly tag. |
Assignment |
An equals sign symbol: = |
Base |
The keyword base |
Break |
The keyword break |
Case |
The keyword case |
Catch |
The keyword catch |
Checked |
The keyword checked |
CloseCurlyBracket |
A closing curly bracket symbol: } |
CloseParenthesis |
A closing parenthesis symbol: ) |
CloseSquareBracket |
A closing square bracket symbol: ] |
|
A colon: : |
Comma |
A comma: , |
Comment |
A comment |
Const |
The keyword const |
Continue |
The keyword continue |
Decrement |
A decrement symbol: -- |
Default |
The keyword default |
Dereference |
A dereference symbol: * |
Do |
The keyword do |
Else |
The keyword else |
EndOfLine |
An end-of-line character: /r or /n |
False |
The keyword false |
Finally |
The keyword finally |
Fixed |
The keyword fixed |
For |
The keyword for |
Foreach |
The keyword foreach |
Get |
The keyword get |
Goto |
The keyword goto |
Increment |
An increment symbol: ++ |
If |
The keyword if |
In |
The keyword in |
Is |
The keyword is |
Lock |
The keyword lock |
Logical |
A logical symbol: !, ||, |, etc. |
Negative |
A negative sign symbol: - |
New |
The keyword new |
Null |
The keyword null |
Number |
A number |
OtherAssignment |
All assignment types other than an equals sign: +=, %=, |=, etc. |
OpenCurlyBracket |
An opening curly bracket symbol: { |
OpenParenthesis |
An opening parenthesis symbol: ( |
OpenSquareBracket |
An opening square bracket symbol: [ |
Other |
An unknown word. |
Out |
The keyword out |
Params |
The keyword params |
Pointer |
A pointer symbol: -> |
Positive |
A positive sign symbol: + |
Preprocessor |
A preprocessor statement. |
QuestionMark |
A question mark symbol: ? |
Readonly |
The keyword readonly |
Ref |
The keyword ref |
Relational |
A relational symbol: ==, !=, etc. |
Remove |
The keyword remove |
Return |
The keyword return |
Semicolon |
A semicolon symbol: ; |
Set |
The keyword set |
Sizeof |
The keyword sizeof |
Stackalloc |
The keyword stackalloc |
String |
A string. |
Switch |
The keyword switch |
This |
The keyword this |
Throw |
The keyword throw |
True |
The keyword true |
Try |
The keyword try |
Typeof |
The keyword typeof |
Unchecked |
The keyword unchecked |
Unsafe |
The keyword unsafe |
Using |
The keyword using |
Value |
The keyword value |
While |
The keyword while |
WhileDo |
The do keyword in a do/while statement |
Whitespace |
A group of back-to-back whitespace characters consisting of spaces and tabs. |
XmlHeader |
An Xml header. |
Yield |
The keyword yield |
Gets the block type that this word appears in.
public CodeBlock.Type
BlockType { get; }
Property Value
The block type that this word appears in.
Gets the line number in the code file where this word ends.
public int EndLineNumber { get; }
Property Value
The line number in the code where this word ends.
Gets a value indicating where the word appears in a block of generated code.
public bool Generated { get; }
Property Value
Indicates whether the word appears in a block of generated code.
Gets the line number in the code file on which the word appears.
public int LineNumber { get; }
Property Value
The line number in the code file on which the word appears.
Gets the text that makes up the word.
public string Text { get; }
Property Value
The text that makes up the word.
Gets the type of the word.
public Word.Type
WordType { get; }
Property Value
The type of the word.
Returns the word text.
public string ToString();
A word that describes a parameter in an anonymous method declaration.
System.Object
Microsoft.Tools.StyleCop.AnonymousParameter
Members
Public Properties |
Description |
The name of the parameter. |
|
The parameter type. |
Gets the name of the parameter.
public Word ParameterName { get; }
Property Value
The name of the parameter.
Gets the type of the parameter.
public string ParameterType { get; }
Property Value
The type of the parameter.
A word that describes a bracket in the code.
System.Object
Microsoft.Tools.StyleCop.BracketWord
Members
Public Types |
Description |
The various types of brackets. |
|
Public Properties |
Description |
The type of the bracket. |
|
The matching bracket for this bracket. |
An enum listing the possible bracket types.
Members
Values |
Description |
CloseCurlyBracket |
A closing curly bracket: } |
CloseParenthesis |
A closing parenthesis: ) |
CloseSquareBracket |
A closing square bracket: ] |
OpenCurlyBracket |
An opening curly bracket: { |
OpenParenthesis |
An opening parenthesis: ( |
OpenSquareBracket |
An opening square bracket: [ |
Gets the type of the bracket.
public BracketWord.Type
BracketType { get; }
Property Value
The type of the bracket.
Gets the matching bracket for this bracket, or null if a matching bracket could not be found.
public BracketWord
MatchingBracket { get; }
Property Value
The matching bracket for this bracket.
A word that describes a group of whitespace characters in the code.
System.Object
Microsoft.Tools.StyleCop.Whitespace
Members
Public Properties |
Description |
The number of tab characters in the whitespace. |
|
The number of tab characters in the whitespace. |
|
Public Methods |
Description |
Returns the whitespace characters as a string. |
Gets the number of space characters in the whitespace.
public int SpaceCount { get; }
Property Value
The number of space characters in the whitespace.
Gets the number of tab characters in the whitespace.
public int TabCount { get; }
Property Value
The number of tab characters in the whitespace.
Returns the whitespace characters as a text string.
public string ToString();
A word that describes a group of whitespace characters in the code.
System.Object
System.Collections.IEnumerable
Microsoft.Tools.StyleCop.WordList
Members
Public Properties |
Description |
Gets the collection of words in the list. |
|
Gets the count of words in the list. |
|
Public Indexers |
Description |
Gets the word at the given index. |
|
Public Methods |
Description |
Gets an enumerator to walk through the words in the collection. |
|
Gets the word at the given index. |
|
Gets the word at the given index without doing any error checking. |
|
Gets a sub-collection of words from the list. |
|
Determines the index of the given word in the collection. |
|
Determines the index of the last matching word in the collection. |
Gets the collection of words in the list.
public ICollection Collection {
get; }
Property Value
The collection of words in the list.
Gets the number of words in the list.
public int Count { get; }
Property Value
The number of words in the list.
Gets the word at the given index.
public Word this[int index];
Parameters
index
The index of the word to retrieve.
Return Value
Returns the word at the given index or null if the index is invalid
Gets an enumerator to walk through the words in the collection.
public IEnumerator GetEnumerator();
Return Value
Returns an enumerator to walk through the words in the collection.
Gets the word at the given index.
public Word GetWord(int index);
Parameters
index
The index of the word to retrieve.
Return Value
Returns the word at the given index or null if the index is invalid
Gets the word at the given index without performing any error checking. This method should only be used when it is guaranteed that the index will not exceed the boundaries of the collection. Otherwise, use the GetWord method.
public Word GetWordEfficient(int index);
Parameters
index
The index of the word to retrieve.
Return Value
Returns the word at the given index or null if the index is invalid
Gets the collection of words specified by the given values.
public WordList GetRange(int index, int count);
Parameters
index
The index of the first word in the collection to return.
count
The number of words to return.
Return Value
Returns the collection of words matching the given parameters.
Gets the index of the first word in the collection that matches the given parameters.
public int IndexOf(Word.Type type);
Parameters
type
The type of word to search for.
Return Value
Returns the index of the first word in the collection that matches the given parameters or -1 if a matching word was not found.
Gets the index of the first word in the collection that matches the given parameters.
public int IndexOf(Word.Type type, string text);
Parameters
type
The type of word to search for.
text
The text of the word to search for.
Return Value
Returns the index of the first word in the collection that matches the given parameters or -1 if a matching word was not found.
Gets the index of the first word in the collection that matches the given parameters.
public int IndexOf(Word.Type type, string text, int start);
Parameters
type
The type of word to search for.
text
The text of the word to search for.
start
The index in the collection at which to being searching.
Return Value
Returns the index of the first word in the collection that matches the given parameters or -1 if a matching word was not found.
Gets the index of the first word in the collection that matches the given parameters.
public int IndexOf(Word.Type type, int start);
Parameters
type
The type of word to search for.
start
The index in the collection at which to being searching.
Return Value
Returns the index of the first word in the collection that matches the given parameters or -1 if a matching word was not found.
Gets the index of the first word in the collection that matches the given parameters.
public int IndexOf(Word.Type type, string text, int start, int count);
Parameters
type
The type of word to search for.
text
The text of the word to search for.
start
The index in the collection at which to being searching.
count
The number of words to search before quitting.
Return Value
Returns the index of the first word in the collection that matches the given parameters or -1 if a matching word was not found.
Gets the index of the last word in the collection that matches the given parameters.
public int LastIndexOf(Word.Type type);
Parameters
type
The type of word to search for.
Return Value
Returns the index of the last word in the collection that matches the given parameters or -1 if a matching word was not found.
Gets the index of the last word in the collection that matches the given parameters.
public int LastIndexOf(Word.Type type, string text);
Parameters
type
The type of word to search for.
text
The text of the word to search for.
Return Value
Returns the index of the last word in the collection that matches the given parameters or -1 if a matching word was not found.
Gets the index of the last word in the collection that matches the given parameters.
public int LastIndexOf(Word.Type type, string text, int start);
Parameters
type
The type of word to search for.
text
The text of the word to search for.
start
The index in the collection at which to being searching.
Return Value
Returns the index of the last word in the collection that matches the given parameters or -1 if a matching word was not found.
Gets the index of the last word in the collection that matches the given parameters.
public int LastIndexOf(Word.Type type, int start);
Parameters
type
The type of word to search for.
start
The index in the collection at which to being searching.
Return Value
Returns the index of the last word in the collection that matches the given parameters or -1 if a matching word was not found.
Gets the index of the last word in the collection that matches the given parameters.
public int LastIndexOf(Word.Type type, string text, int start, int count);
Parameters
type
The type of word to search for.
text
The text of the word to search for.
start
The index in the collection at which to being searching.
count
The number of words to search before quitting.
Return Value
Returns the index of the last word in the collection that matches the given parameters or -1 if a matching word was not found.
Describes a block of code in the file.
System.Object
Microsoft.Tools.StyleCop.CodeBlock
Members
Public Types |
Description |
The various types of code blocks |
An enum listing the possible code block types.
Members
Values |
Description |
AssemblyTag |
An assembly tag |
Code |
Regular code |
DoubleQuote |
A double-quoted string |
MultiLineComment |
A comment that begins and ends with /* and */ |
Preprocessor |
A preprocessor tag. |
SingleLineComment |
A comment beginning with // |
SingleQuote |
A single-quoted string |
UnsafeCode |
Code marked unsafe code |
XmlHeader |
An Xml header that appears above a code element |
A document object that models a code file and can be used to edit the contents of the file.
System.Object
Microsoft.Tools.StyleCop.CodeDocument
Members
Public Methods |
Description |
Replaces a portion of code in the document with the new text provided. |
Replaces a portion of code in the document with the new text provided.
public bool UpdateCode(int line, int offset, int count, string text);
Parameters
line
The line in the file where the text replacement will occur.
offset
The offset from the beginning of the line where the replacement will begin.
count
The number of characters to replace.
text
The text to replace the existing code with.
Return Value
Returns true if the text was replaced successfully.
Interface which must be implemented by a page that appears on the StyleCop settings dialog. The page settings should be loaded and saved using the GetProjectProperty and SaveProjectProperty methods on the Host property of the current StyleCopCore object.
Members
Public Properties |
Description |
Indicates whether the page is dirty. |
|
The text that will appear on the page’s tab. |
|
Public Methods |
Description |
Called whenever the page is shown or hidden. |
|
Saves the data on the page. |
|
Initialize the page. |
Gets a value indicating whether the page is dirty.
public bool Dirty { get; }
Property Value
Indicates whether the page is dirty.
Remarks
Whenever the dirty state of a page changes, the page must call the IPropertyControlHost.DirtyChanged method to cause the property page control to enable or disable the Apply button appropriately.
Gets the text that will appear on the page’s tab.
public string TabName { get; }
Property Value
Gets the page’s tab name.
Called when the page is shown or hidden.
public void Activate(bool activated);
Parameters
activated
Indicates whether the control is being activated or deactivated.
Causes the page to save its dirty data.
public void Apply();
Initializes the page. This is called when the page is first loaded.
public void Initialize(IPropertyControl propertyControl, object
context);
Parameters
propertyControl
Interface into the property page control that is hosting this page.
context
Provides an array of objects. The first item in the array is a reference to the StyleCopCore object. The second item in the array is a string containing the ID of the project being edited.
Interface into the property page control that hosts the StyleCop settings pages.
Members
Properties |
Description |
Gets the list of pages currently loaded into the control. |
|
Gets the currently active page. |
|
Methods |
Description |
Causes the property page control to update the status of the Apply button. |
Gets the list of pages currently loaded into the control.
public IPropertyControlPage[] Pages { get; }
Property Value
The list of pages currently loaded into the control.
Gets the currently active page on the control.
public IPropertyControlPage ActivePage { get; }
Property Value
The currently active page on the control.
Causes the property page control to update the enabled status of the Apply button based on whether any of the pages is currently dirty.
public void DirtyChanged();
The main StyleCopCore engine object.
System.Object
Microsoft.Tools.StyleCop.StyleCopCore
Members
Public Properties |
Description |
Indicates whether the current analyze run should cancel. |
|
Gets the collection of add-ins currently loaded into the tool. |
|
An interface into the object hosting StyleCopCore. |
Gets or sets a value indicating whether the currently analyze run should be cancelled. Add-ins should check this value periodically to determine whether to cancel their currently running analyze operation.
public bool Cancel { get; set; }
Property Value
Indicates whether the cancel the current analyze run.
Gets the collection of add-ins currently loaded into the tool.
public ICollection Addins { get; }
Property Value
The collection of StyleCopAddin objects currently loaded into the tool.
Gets the interface into the object that is hosting the StyleCopCore object.
public IStyleCopHost
Host { get; }
Property Value
The interface into the object that is hosting the StyleCopCore object.
An interface into the object that hosts StyleCopCore.
Members
Methods |
Description |
Gets the given property value from the given project. |
|
Sets the given property value in the given project. |
Gets the value of the given property from the specified project.
public string
GetProjectProperty(string projectId, string name);
Parameters
projectId
The ID of the project to retrieve the property value from.
name
The name of the property to retrieve from the project.
Saves the value of the given property in the specified project.
public void
SaveProjectProperty(string projectId, string name, string property);
Parameters
projectId
The ID of the project to save the property value in.
name
The name of the property to save in the project.
property
The value to save into the property.
Enum that describes the various access levels for C# elements.
Members
Values |
Description |
Internal |
An internal element that is visible to everyone within the same assembly, but no one outside of the assembly. |
Private |
A private element that is only visible to other class members. |
Protected |
A protected element that is only visible to other class members and members of a class inheriting the class that this element belongs to. |
ProtectedAndInternal |
A protected element that is inside of an internal element. |
ProtectedInternal |
An element that is marked protected internal. |
Public |
A public element that is visible to everyone. |
The following sample code implements a StyleCop add-in that checks to make sure all member variables begin with lower-case letters, and all other elements begin with upper-case elements, including const or readonly member variables.
using System;
using System.Collections;
using System.Globalization;
using
Microsoft.StatusMonitor.StyleCop;
namespace NameChecker
{
/// <summary>
/// The list of violation types that can be found by this add-in
module
/// </summary>
public
enum ViolationType : int
{
WordStartsWithUpper = 0,
WordStartsWithLower
}
/// <summary>
///
Checks the case of the first letter of element names
/// </summary>
public
class NameCase : StyleCopAddin
{
/// <summary>
/// Constructor
/// </summary>
public
NameCase() :
base("5ebc1cb7-6f5c-484a-b208-fd4c44733acb", "Name
case", ViolationList.ItemsArray)
{
}
/// <summary>
/// Checks the elements within the given file
/// </summary>
/// <param
name="file">The file to check</param>
public
override void
ProcessFile(CodeFile file)
{
this.ProcessElement(file.Contents);
}
/// <summary>
/// Processes one element and its children
/// </summary>
/// <param name="element">The
element to process</param>
private
void ProcessElement(Element element)
{
switch
(element.Type)
{
case
Element.Type.Namespace:
case
Element.Type.Class:
case
Element.Type.Enum:
case
Element.Type.Struct:
case
Element.Type.Delegate:
case
Element.Type.Event:
case
Element.Type.Property:
case
Element.Type.Method:
this.CheckCase(element, true);
break;
case
Element.Type.MemberVariable:
this.CheckCase(element, Common.IsConst(element));
break;
default:
break;
}
foreach
(Element child in element.Children)
{
this.ProcessElement(child);
}
}
/// <summary>
/// Checks the case of the first character in the given word
/// </summary>
/// <param name="element">The
element that the word appears in</param>
/// <param
name="upper">True if the
character should be upper, false if it should be lower</param>
private
void CheckCase(Element element, bool upper)
{
string
first = element.RealName.Substring(0, 1);
if
(upper)
{
if
(first != first.ToUpper())
{
base.AddViolation(element, element.LineNumber,
ViolationType.WordStartsWithLower, element.RealName);
}
}
else
{
if
(first != first.ToLower())
{
base.AddViolation(element, element.LineNumber,
ViolationType.WordStartsWithUpper, element.RealName);
}
}
}
}
/// <summary>
/// The violation data
/// </summary>
internal
class ViolationList
{
/// <summary>
/// An array of all the violations
/// </summary>
private
static readonly
ViolationListItem[] Items =
{
new
ViolationListItem((int)ViolationType.WordStartsWithUpper, "Element must
start with a lower-case letter: {0}"),
new
ViolationListItem((int)ViolationType.WordStartsWithLower,
"Element must start with an upper-case letter:
{0}")
};
/// <summary>
/// Gets the violations array
/// </summary>
public
static ViolationListItem[] ItemsArray
{
get
{ return Items; }
}
}
}