Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# location of import statements, making StyleCop happy

I am a big fan of StyleCop, it makes my life easier. A bunch of other people have thought of good rules, and I gladly follow them by enabling StyleCop. Recently I have been messing with Coded Ui extensibility, and came across this article:

http://blogs.msdn.com/b/gautamg/archive/2010/01/05/2-hello-world-extension-for-coded-ui-test.aspx

The sample code below makes StyleCop unhappy because the using statements are outside of the namespace. However, I can move only the two System* packages in - the third is needed to define an assembly attribute, and I cannot throw assembly: inside of a namespace.

Is there a clean way to re-organize this code?

using System;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UITest.Common;
using Microsoft.VisualStudio.TestTools.UITest.Extension;

// Attribute to denote that this assembly has UITest extensions.
[assembly: UITestExtensionPackageAttribute("HelloWorldPackage",
           typeof(UITestHelloWorldPackage.HelloWorldPackage))]

namespace UITestHelloWorldPackage
{
    internal class HelloWorldPackage : UITestExtensionPackage
    {
        public override object GetService(Type serviceType)
        {
            Trace.WriteLine("Hello, World");
            return null;
        }
....
like image 668
Hamish Grubijan Avatar asked Feb 22 '11 15:02

Hamish Grubijan


2 Answers

IMHO, the rule to put usings inside the namespace is useless and makes the code hard to read.

like image 60
Daniel Hilgarth Avatar answered Sep 30 '22 01:09

Daniel Hilgarth


Can't you move your [assembly: UITestExtensionPackageAttribute()] attribute to your Properties\AssemblyInfo.cs file? I'm guessing the article you referenced had the [assembly:] attribute there just to keep the example in a single block of code.

I agree with Daniel - having using statements inside namespaces makes your code harder to read.

I'd recommend moving the attribute to your AssemblyInfo.cs file and keep your usings at the top of your class file. That seems pretty standard.

Hope this helps!

like image 28
David Hoerster Avatar answered Sep 30 '22 01:09

David Hoerster