How to use IssueProvider DXCore component
Let’s assume that we need to highlight all local variables with incorrect names using CodeRush Code Issues technology. For example, the name is incorrect if it starts from an upper case letter instead of a lower case letter. Follow these steps to get the IssueProvider component working:
- Create a new DXCore plug-in if you haven’t done it yet
- Drop the IssueProvider control on the plug-in designer surface
- Fill the ProviderName property of the IssueProvider
- Fill the Description property of the IssueProvider if needed
- Subscribe to the CheckCodeIssues event:
CSharp code:
private void issueProvider1_CheckCodeIssues(object sender, CheckCodeIssuesEventArgs ea)
{
}
Visual Basic code:
Private Sub IssueProvider1_CheckCodeIssues(ByVal sender As Object, ByVal ea As CheckCodeIssuesEventArgs) Handles IssueProvider1.CheckCodeIssues End Sub
- Next, add a new Class into your project and make it a descendant of the BaseCodeIssueSearcher
- Override its CheckCodeIssues method
- In the CheckCodeIssues event handler type the following code:
CSharp code:
private void issueProvider1_CheckCodeIssues(object sender, CheckCodeIssuesEventArgs ea)
{
MyIssuesSearcher searcher = new MyIssuesSearcher();
searcher.CheckCodeIssues(ea);
}
Visual Basic code:
Private Sub IssueProvider1_CheckCodeIssues(ByVal sender As Object, ByVal ea As CheckCodeIssuesEventArgs) Handles IssueProvider1.CheckCodeIssues Dim searcher As New MyIssuesSearcher() searcher.CheckCodeIssues(ea) End Sub
Now, inside the CheckCodeIssues overridden method we need to implement our issues searcher logic:
CSharp code:
public class MyIssuesSearcher : BaseCodeIssueSearcher
{
public override void CheckCodeIssues(CheckCodeIssuesEventArgs ea)
{
// Specify elements to work with - variables in this case...
LanguageElementType[] types = new LanguageElementType[] { LanguageElementType.Variable, LanguageElementType.InitializedVariable, LanguageElementType.ImplicitVariable };
ElementTypeFilter elementFilter = new ElementTypeFilter(types);
// Enumerate the scope (a source file) with the specified elements...
IEnumerable enumerable = ea.GetEnumerable(ea.Scope, elementFilter);
foreach (IElement element in enumerable)
{
// Let's work with the correct variable element instance...
IVariableDeclarationStatement variableDeclaration = element as IVariableDeclarationStatement;
if (variableDeclaration == null)
continue;
// Check if it is a local variable...
if (!variableDeclaration.IsLocal)
continue;
// Check its name...
string variableName = variableDeclaration.Name;
if (!String.IsNullOrEmpty(variableName))
if (variableName.Length > 0)
if (Char.IsUpper(variableName[0]))
ea.AddWarning(variableDeclaration.FirstNameRange, "Incorrect variable name");
}
}
}
Visual Basic code:
Public Class MyIssuesSearcher
Inherits BaseCodeIssueSearcher
Public Overrides Sub CheckCodeIssues(ByVal ea As CheckCodeIssuesEventArgs)
' Specify elements to work with - variables in this case...
Dim types As LanguageElementType() = New LanguageElementType() {LanguageElementType.Variable, LanguageElementType.InitializedVariable, LanguageElementType.ImplicitVariable}
Dim elementFilter As New ElementTypeFilter(types)
' Enumerate the scope (a source file) with the specified elements...
Dim enumerable As IEnumerable(Of IElement) = ea.GetEnumerable(ea.Scope, elementFilter)
For Each element As IElement In enumerable
' Let's work with the correct variable element instance...
Dim variableDeclaration As IVariableDeclarationStatement = TryCast(element, IVariableDeclarationStatement)
If (variableDeclaration Is Nothing) Then Continue For
' Check if it is a local variable...
If (Not variableDeclaration.IsLocal) Then Continue For
' Check its name...
Dim variableName As String = variableDeclaration.Name
If (Not String.IsNullOrEmpty(variableName)) Then
If (variableName.Length > 0) Then
If (Char.IsUpper(variableName(0))) Then
ea.AddWarning(variableDeclaration.FirstNameRange, "Incorrect variable name")
End If
End If
End If
Next
End Sub
End Class
That’s it.
- Compile the plug-in
- Open a new instance of the Visual Studio IDE
- Load IDE tools if they’re not loaded automatically
- Make sure Code Issues Analysis is turned on
- Open any project and check how the new code issue works:

The sample plug-in with the source code is attached (38,051 bytes, C# and VB, Visual Studio 2010). If you have any issues or need any further assistance – you are always welcome to contact DevExpress Support Services or me directly.
This post is a part of post series about CodeRush Code Issues technology. To learn more about code issues, please refer to the appropriate topics:
- Code Issues technology overview
- Code Issues types overview
- Code Issues visual presentation
- Code Issues fixes and suppression
- Code Issues navigation techniques
- Code Issues tool window
- Code Issues configuration and options
- Code Issues overall list
- Creating your own code issue (current post)
Similar Posts:
- How to use CodeMetricProvider control to build you own code metric
- Creating a new code issue and its fix for Java Script
- How to get the declaring element (declaration) from a reference using DXCore
- How to add new contracts for the Add Contract code provider
- Creating a simplest refactoring using the RefactoringProvider control
Hi Alex; can you say where BaseCodeIssuesSearcher is located? I’m using dx2010.2, but reference is not found.
Ahh, “BaseCodeIssuesSearcher” is mis-named. Should be BaseCodeIssueSearcher in CodeRush.Core.
@Robert Leahey Hi Robert, thanks for noticing this – it is corrected now.
I’ve been able to follow your steps in creating a new plugin with an issue provider. After following your steps however, I find that the CheckCodeIssues has an enumerable variable that shows a message “Enumerable yielded no results” (when debugging the code) after loading the plugin and loading a .cs file that includes local variables. As a result, the code inside the for loop never runs. Any suggestions?
@Mike Berenson
I have noticed that after I tried to test the code once again. I did a small correction in the code to make it always work (on the 10th of Dec). Are you using the corrected code? (See the first two code lines inside of the CheckCodeIssues method).
Excellent, thanks for the heads up on the corrected code Alex. It works great now.