Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Directives, Namespace and Assembly Reference - all jumbled up with StyleCop!

I like to adhere to StyleCop's formatting rules to make code nice and clear, but I've recently had a problem with one of its warnings:

All using directives must be placed inside of the namespace.

My problem is that I have using directives, an assembly reference (for mocking file deletion), and a namespace to juggle in one of my test classes:

using System;
using System.IO;
using Microsoft.Moles.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[assembly: MoledType(typeof(System.IO.File))]

namespace MyNamespace
{
//Some Code
}

The above allows tests to be run fine - but StyleCop complains about the using directives not being inside the namespace.

Putting the usings inside the namespace gives the error that "MoledType" is not recognised.

Putting both the usings and the assembly reference inside the namespace gives the error

'assembly' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are 'type'. All attributes in this block will be ignored.

It seems I've tried every layout I can but to no avail - either the solution won't build, the mocking won't work or StyleCop complains!

Does anyone know a way to set these out so that everything's happy? Or am I going to have to ignore the StyleCop warning in this case?

like image 774
Jack Avatar asked Jun 18 '10 13:06

Jack


1 Answers

Solved two minutes later!

I just needed to put the full path of "MoledType" in the assembly reference - meaning I could leave it outside of the namespace with the using directives inside like so:

[assembly: Microsoft.Moles.Framework.MoledType(typeof(System.IO.File))]
namespace MyNamespace
{
using System;
using System.IO;
using Microsoft.Moles.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;

// Some Code...
}

Hopefully someone'll find this useful!

like image 58
Jack Avatar answered Oct 05 '22 22:10

Jack