Extract from
Clean Coders StyleCop Rules
Your cast seems bad. You are looking at a syntax similar to this:
this.StyleCopConsole.ViolationEncountered += (sender, args) => this.StyleCopViolations.Add(args.Violation);
namespace CleanCodersStyleCopRules.Test
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using NUnit.Framework;
using StyleCop;
[TestFixture]
public abstract class TestFixtureBase
{
#region Constants and Fields
private CodeProject codeProject;
#endregion
#region Constructors and Destructors
protected TestFixtureBase()
{
string settings = AppDomain.CurrentDomain.BaseDirectory + @"\Resources\Settings.StyleCop";
List<string> addInPath = new List<string> { AppDomain.CurrentDomain.BaseDirectory };
this.StyleCopConsole = new StyleCopConsole(settings, false, null, addInPath, true);
this.StyleCopConsole.ViolationEncountered += (sender, args) => this.StyleCopViolations.Add(args.Violation);
this.StyleCopConsole.OutputGenerated += (sender, args) => this.StyleCopOutput.Add(args.Output);
}
#endregion
#region Properties
protected List<string> StyleCopOutput { get; private set; }
protected List<violation> StyleCopViolations { get; private set; }
private StyleCopConsole StyleCopConsole { get; set; }
#endregion
#region Public Methods and Operators
[SetUp]
public void Setup()
{
this.StyleCopViolations = new List<violation>();
this.StyleCopOutput = new List<string>();
string settings = AppDomain.CurrentDomain.BaseDirectory + @"\Resources";
Configuration configuration = new Configuration(new string[0]);
this.codeProject = new CodeProject(Guid.NewGuid().GetHashCode(), settings, configuration);
}
[TearDown]
public void TearDown()
{
this.codeProject = null;
this.StyleCopViolations.Clear();
this.StyleCopOutput.Clear();
}
#endregion
#region Methods
protected void AnalyzeCodeWithAssertion(string sourceCodeFileName, int numberOfExpectedViolation)
{
this.AddSourceCode(sourceCodeFileName);
this.StartAnalysis();
this.WriteViolationsToConsole();
this.WriteOutputToConsole();
Assert.AreEqual(numberOfExpectedViolation, this.StyleCopViolations.Count);
}
protected void WriteViolationsToConsole()
{
foreach (Violation violation in this.StyleCopViolations)
{
Console.WriteLine(violation.Message);
}
}
private void AddSourceCode(string sourceCodeFileName)
{
string fullSourceCodeFilePath = AppDomain.CurrentDomain.BaseDirectory + @"\Resources\" + sourceCodeFileName;
bool result = this.StyleCopConsole.Core.Environment.AddSourceCode(this.codeProject, fullSourceCodeFilePath, null);
if (result == false)
{
throw new ArgumentException("Source code file could not be loaded.", sourceCodeFileName);
}
}
private void StartAnalysis()
{
CodeProject[] projects = new[] { this.codeProject };
bool result = this.StyleCopConsole.Start(projects, true);
if (result == false)
{
throw new ArgumentException("StyleCopConsole.Start had a problem.");
}
}
private void WriteOutputToConsole()
{
Console.WriteLine(string.Join(Environment.NewLine, this.StyleCopOutput.ToArray()));
}
#endregion
}
}
</string></violation></violation></string></string></string>