I installed StyleCop and the associated plugin for ReSharper 5. After getting annoyed with it I removed both the plugin and StyleCop, but ReSharper is still using some of the StyleCop behaviour - most notably moving using statements to within the namespace declaration, rather than keeping them outside the declaration.
For instance say you have the following source:
using System;
using System.Web;
namespace Foo.Bar
{
////
}
And the file sits within The Foo/Bar/Widget directory, using ReSharper's fix namespace tool I would expect the file to stay the same, but the namespace to have changed to Foo.Bar.Widgets (this is the behaviour it exhibited before StyleCop came along).
Now however it rearranges the file:
namespace Foo.Bar.Widget
{
using System;
using System.Web;
////
}
Now putting aside people's personal preferences about which one is better, I don't like it, and it is inconsistent with our existing code. Having to manually move using statements after renaming the namespace takes long than renaming the namespace manually.
Does anyone know how to correct this (I'm assuming there is a file or something still lingering around from the install, or a config that hasn't been reverted).
You can change it here:
ReSharper -> Options -> Languages -> C# -> Namespace Imports -> Add using directive to the deepest scope
UPDATE - Resharper 9 This option is now moved to:
ReSharper -> Options -> Code Editing -> C# -> Code Style -> Reference qualification
I just had the same issue. It turns out that the StyleCop settings are stored in the "This computer" layer of ReSharper settings. See ReSharper > Manage Options for a list of layers.
I just had to reset the "This computer" layer; this was possible since I had never intentionally modified it.
Related
We're trying SonarLint with VS2015 Enterprise and have an irritating problem which could be a show-stopper unless we resolve it. Core i5 processor, 8GB memory, large SSD, Windows 7 Pro:
We have masses of legacy warnings of the same warning code (eg S1444). I can live with the existing code but want to catch them for future work, so I want to suppress the existing warnings but without messy pragmas in the code. So, I multi-select them all in the Error List screen and rt-click, then click In Suppression File. A GlobalSuppressions file is created in the appropriate projects and updated.
However the warnings continue to be displayed. I clean and rebuild the solution and the 'suppressed' warnings continue to be displayed, still with a suppression state of 'Active' (which I am guessing means 'not suppressed').
This means it is almost (or may actually be) impossible to eliminate old warnings so that new ones are clearly exposed, which is the whole point of the exercise.
Is this a bug or am I missing something?
Also I notice that sometimes the rt-click context menu includes Suppress-> and other times it doesn't. How does that work?
I can't reproduce the issue you are facing. I have created a ConsoleApplication (C#) and used the default code. I have then selected all issues, right-click and Suppress in Suppression File. I tried to rebuild, clean, restart VS and warnings no longer show up.
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication14
{
class Program
{
static void Main(string[] args)
{
}
}
}
GlobalSuppression.cs
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Sonar Code Smell", "S1172:Unused method parameters should be removed", Justification = "<Pending>", Scope = "member", Target = "~M:ConsoleApplication14.Program.Main(System.String[])")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Sonar Code Smell", "S1186:Methods should not be empty", Justification = "<Pending>", Scope = "member", Target = "~M:ConsoleApplication14.Program.Main(System.String[])")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Sonar Code Smell", "S1118:Utility classes should not have public constructors", Justification = "<Pending>", Scope = "type", Target = "~T:ConsoleApplication14.Program")]
I am using the latest SonarLint version (2.9.0.384). Could you create a simple repro case so I can work with it to find out what's happening?
I'm using StyleCop and FxCop tools to improve my code but I came to a place where there are two rules, one in StyleCop and one in FxCop that exclude each other. If I fix my code to match the rule from StyleCop then FxCop validation fails and vice versa.
First rule is SA1200 from StyleCop which says that all using directives must be placed inside of the namespace.
All using directives must be placed inside of the namespace.
So I have done something like this
namespace MyNamespace
{
using System;
...
}
It was ok for StyleCop, no more warnings. Now I run FxCop validation and it tells me that CA1014 is violated:
Mark 'MyApp.dll' with CLSCompliant(true) because it exposes externally visible types.
To resolve this I should do something like this:
[ClsCompliant(true)]
namespace MyNamespace
{
...
}
but now I cannot build my project because ClsCompliant attribute is not recognized (because it's from System namespace which I include inside of the MyNamespace). So if I move using System; directive outside of MyNamespace declaration. This will make my code compile but again it will break the rule from StyleCop.
Is there any way to deal with this problem except for disabling one of the rules in StyleCop or FxCop? And if that's not possible which of the rules should I disable? Which is less important?
Use full attribute name:
[System.CLSCompliant(true)]
namespace MyNamespace
{
...
}
BTW: if you want to mark your whole assembly as CLSCompliant, put
[assembly: System.CLSCompliant(true)]
in Properties/AssemblyInfo.cs file
My suggestion is to turn off the "All using directives must be placed inside of the namespace." rule in StyleCop. It's impractical to adhere to it, especially since most of the code generators (even VS own ones) do not follow this practice.
I am working on a REST WCF project and when I implement the following code, it complains that it can't resolve the WebGet class? What am I missing?
I tried importing the System.ServiceModel.Web namespace but it can't find it even though I referenced it. The "Web" in System.ServiceModel.Web does not register when I register it in a using statement on top of my code.
Basically, what do I need to implement such WCF REST concepts like WebGet, WebInvoke, UriTemplate, etc?
UPDATE: After some feedback and thinking about this a little bit more what I've done, it seems that the DLLs (System.ServiceModel & System.ServiceModel.Web) do not come up via the 'Add Reference' window when I go to add a project reference. When I first started the project, FYI, since these assemblies did not come up at first, I went 'searching' for them, and copied them to a temp folder so I can reference them and thus, I guess I am having the resolve issues. So, now that I am at this point, how can I get my VS to recognize/register these WCF REST DLLs? Thanks!
UPDATE: I believe I am update-to-date on everything: developing on VS 2008 SP1 - I try to download the latest SPs, downloaded the REST Preview 2 Starter Kit, developing against 3.5 Framework, trying to create a WCF REST layer to ultimately be consumed by Silverlight 2 client.
This is what I have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using UtilityClasses;
using Microsoft.ServiceModel.Web;
using Microsoft.Http;
namespace WcfRestService
{
[ServiceContract]
public interface IRestService
{
[OperationContract(Name = "Add")]
[WebGet(UriTemplate = "/")] // ** can't compile here **
int Add();
}
}
Any advice will be greatly appreciated it.
You need to reference the System.ServiceModel.Web DLL.
Right-click the 'References' folder in your project and choose 'Add Reference...'. Scroll down to System.ServiceModel.Web and click 'OK'.
Just a one thought, you might be targeting your project to .Net Client Profile which exposes limited namespaces. you may need to check the target framework setting at your project properties.
I have faced that with a WCF project not finding System.ServiceModel.Web untill I changed the default framework settings.
HTH
This happened to me too.
I did this:
Delete System.Service.Web from References
Build
Clean Project
Add System.Service.Web to References
Build
..and VS found it??
In "project properties" make sure your "target framework" is set to : .NET Framework 4
and not: .NET Framework 4 Client Profile, or any lower .NET version.
Also, if possible use VS 2010.
--DBJ
right click on the project name and choose Properties.
change the target framework to .NET Framework 4.
right click on the References and choose Add Reference.
And then you can see System.ServiceModel.Web.
By default the target framework is .NET Framework 4 Client Profile,
so you cannot find the System.ServiceModel.Web.
I had the same problem.
I have added this missing reference:
System.ServiceModel.Web
and this code line:
using System.ServiceModel.Web;
and all got solved! ;)
using System.ServiceModel.Web;
In my case my project was building despite this warning in the designer view of the service class. Not really a big issue, but still pretty annoying. Realised it was just ReSharper playing up - it hadn't updated its internal cache when the reference to System.ServiceModel was added automatically by VS when I added a new WCF Service. I turned off real-time code analysis in:
Tools -> Options -> Resharper Ultimate -> Options -> Code Inspection->
Settings -> Enable code analysis
This restored the built-in VS code analysis, and problem was fixed straight away.
If you'd prefer to keep using ReSharper code analysis, clearing the cache in:
Tools -> Options -> Resharper Ultimate -> Options -> Environment ->
General -> Clear Caches
may also sort the issue.
Is there a way to reference a namespace globally across the whole solution?
So instead of having these lines in every code file:
using System;
using MyNamespace;
having to declare them only once, and every code file would use them.
Btw I am using Visual Studio.
No, C# doesn't have this concept. Each source file is independent in this respect. (And if the using directives are in a namespace declaration, those are independent from other using directives in peer namespace declarations, too. That's a pretty rare case though in my experience.)
You don't need ReSharper to change what gets included in a new class though. You can use the Visual Studio templates.
EDIT: Just to clarify the point about using directives within namespaces, suppose we had (all in one file):
using Foo;
namespace X
{
using Bar;
// Foo and Bar are searched for code in here, but not Baz
}
namespace Y
{
using Baz;
// Foo and Baz are searched for code in here, but not Bar
}
Usually I only have one namespace declaration in a file, and put all the using directives before it.
No, this is not possible.
If you're using ReSharper, you can set an option to include specific using directives in every new file you create though.
From this SO question and follow-up blog post. You can edit the Visual Studio default templates.
To do this, look at the file in this zip : [Program Files][Visual Studio]\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
and modify the Class.cs file as needed. Additionally, Visual Studio may have cached this file here :
[Program Files][Visual Studio]\Common7\IDE\ItemTemplatesCache\CSharp\Code\1033\Class.zip
In C# 10.0 you can use Global Usings.
global using System;
global using MyNamespace;
No, you can not reference a namespace globally across the whole solution in .NET or .NET CORE.
But you can use project wise namespace globally in solution. this feature will be available from c#10/.NET 6. currently it's in preview but it will be released in NOV 2021
=========Project level .NET 6 global using namespace=========
Create a class file at root of the project e.g GlobalNamespace.cs
global using System;
global using System.Linq;
global using System.Text.RegularExpressions;
global using System.Threading.Tasks;
Then you don't need to declare using namespace in other .cs files of the project which are already declared globally.
As others have mentioned Visual Studio Templates are the way to go.
Note that simply adding a using statement to your template will not ensure that the compiler can resolve your types. So, if you are adding a using statement for MyNamespace in every class you may need to add an assembly reference to your project as well. See the C# FAQ for more information.
One trick I miss as a newb to CSharp is to look at the "refences" (in VS), to right click and "Add New Reference". This is especially handy when combining mulitple projects where I have made some generic class for reuse elsewhere.
I'm getting this error
The type or namespace name 'DataVisualization' does not exist in the namespace 'System.Windows.Forms' (are you missing an assembly reference?)
Here is my using section of the class:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms.DataVisualization.Charting;
using System.Windows.Forms.DataVisualization.Charting.Borders3D;
using System.Windows.Forms.DataVisualization.Charting.ChartTypes;
using System.Windows.Forms.DataVisualization.Charting.Data;
using System.Windows.Forms.DataVisualization.Charting.Formulas;
using System.Windows.Forms.DataVisualization.Charting.Utilities;
namespace myNamespace {
public class myClass {
// Usual class stuff
}
}
The thing is that I am using the same DataVisualization includes in another class. The only thing that I can think that is different is that the classes that are giving this missing namespace error are Solution Items rather than specific to a project. The projects reference them by link. Anyone have thoughts on what the problem is? I've installed the chart component, .Net 3.5 SP1, and the Chart Add-in for Visual Studio 2008.
UPDATE: I moved the items from Solution Items to be regular members of my project and I'm still seeing the same behavior.
UPDATE 2: Removing the items from the Solution Items and placing them under my project worked. Another project was still referencing the files which is why I didn't think it worked previously. I'm still curious, though, why I couldn't use the namespace when the classes were Solution Items but moving them underneath a project (with no modifications, mind you) instantly made them recognizable. :\
You are very likely missing a reference to the DataVisualization DLL. Note that although they share the namespace of System.Windows.Forms.dll, they aren't actually contained within it.
Solution items aren't used by compiled assemblies.
http://msdn.microsoft.com/en-us/library/1ee8zw5t.aspx
"They can be referenced by projects, but are never included in solution or project builds"
As far as I know, solution folders/items are really just meant for organizing things.
Are you getting actual build errors or just squiggles? Try building and look at the output window, does it succeed or fail?
In VS 2008 SP1 C# introduced a top level error squiggling feature. It's possible that if you open the solution item version of the file it will squiggle because of a lack of default references. The solution should still build correctly though.
If this is not the case try adding the file directly to the project (no link). See if that eliminates the error. If so then we know it has to due with a linked file and it can help track down the problem.