I have a Silverlight project with a generated Reference.cs file where the service reference is in. The class is attributed with [GeneratedCode] and in the project configuration the code analysis on generated code is disabled (Release and Debug).
What have I done wrong?
Maybe you should try the solutions that work for StyleCop:
Put ".Designer.cs" to the end of the name of the file you don’t want StyleCop to check. Or call the the [sic] class, and the file containing it, "NativeMethods". Make sure you also uncheck "Analyze designer files" in StyleCop settings. In this case the whole file will be bypassed. You don’t have to do so for some types of Microsoft designer-generated code, like Windows Forms Designer, because they automatically fall under conditions of the following option:
Surround the undesired piece of code with a C# region containing "generated code" in its name. StyleCop does not check generated code by default (make sure the "Analyze generated files" setting is not checked, though). In this case you can still validate the names of the fields generated for the Windows Forms controls.
#region Windows Form Designer generated code
...
#endregion
To ignore the whole generated file, check whether your generator puts an "<auto-generated />" XML element into the StyleCop-conform file header, like the following:
// <auto-generated />
And finally, you can set to true the "ExcludeFromSourceAnalysis" property of the MSBuild Compile item that represents the file needed to be excluded from analysis. It only works if you use the provided "Microsoft.SourceAnalysis.Targets" targets file, otherwise you have to feed the StyleCop MSBuild task with desired source files on your own.
Source: https://shishkin.wordpress.com/2008/07/08/stylecop-how-to-ignore-generated-code/
Related
I've seen answers showing how to suppress a warning for a specific line of code or for a specific project. I don't want that.
I want to suppress a specific warning for all of my projects.
(If it matters, the warning is IDE0044. And I'm using C#.)
A recent update to Visual Studio 2017 (15.7.1) has an option for this now. Under the Tools->Options menu, select the TextEditor->C#->Code Style->General tab. Under Field preferences, there is a Prefer readonly option. Set that to No.
There is also an editorconfig setting you can set if you want to check this preference in along side your code, so others who consume your code don't get the warning, but that has to be done on a per solution basis. The editorconfig value you would set would be:
dotnet_style_readonly_field = false:none
You can use the SuppressMessage attribute present under System.Diagnostics.CodeAnalysis namespace like
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "args")]
Well as you have edited saying I want to suppress a specific warning for all of my projects
You can't do that for a entire project wise AFAIK. But check the linked post once if that helps
How to suppress code analysis messages for all type members?
To suppress warnings for all projects, you need to create a .editorconfig file in a top-level directory. For example, I have mine in trunk and I commit it to source control so that my colleagues share the same settings.
The settings in this file apply to all projects in trunk and subfolders, unless overridden by another .editorconfig file further down the folder tree e.g. you might you have a project specific EditorConfig file in a subfolder which has different settings. See File hierarchy and precedence for more details.
Creating an EditorConfig file
You can use a text editor for this if you just want to change one specific setting. However, Visual Studio can create a .editorconfig file with sensible defaults for .NET for you. From MSDN:
Create a new project
From the menu bar, choose Project > Add New Item; or press Ctrl+Shift+A
Select the editorconfig File (.NET) template to add an EditorConfig file prepopulated with default .NET code style, formatting, and naming conventions
Optionally delete the project - we don't really need it
Visual Studio 2019 - Creating an EditorConfig file from current settings
In Visual Studio 2019, you can instead create an EditorConfig file from your current settings. Just click the following button in the Options dialog under Text Editor > C# > Code Style > General:
If you're creating in a text editor you'll probably need this at the top of the file, adjusted as necessary:
# Remove the line below if you want to inherit .editorconfig settings from higher directories
root = true
# C# files
[*.cs]
Disabling IDE0044 in the editor config file
To disable IDE0044 specifically, add or change the following setting in the .editorconfig file:
dotnet_style_readonly_field = false:none
(In Visual Studio 2019, you can set the Prefer readonly option to No under TextEditor-> C# -> Code Style-> General in Options and then press the Generate .editorconfig file from settings button as detailed above).
You may try to use Directory.Build.props adding NoWarn property for specific warnings. I haven't verified it though.
And as it's said in another answer, it's better to fix the root cause instead of ignoring it.
I would like to add to Stephen's post that his solution with the .editorconfig file didn't work out for me without specifying the files I want to apply the rule to. For example, and given that I want to apply a rule to all the test files and that I follow a naming convention in which these end up with "Tests.cs", I have managed to ignore the CA1707 rule in those files by using the following rule:
[*Tests.cs]
dotnet_diagnostics.CA1707.severity = none
More information on my answer here
IDE0044 is "add readonly modifier" so why not just add the modifier?
Warnings are telling you that you're doing something wrong, but the app will compile.
It's best to have zero warnings in an ideal world.
I'm having trouble understanding a behaviour of Nuget. I've created a basic package that deploys a single Test.cs file into an "HtmlHelpers" folder in a project. I'm following the "convention based working directory" method described on the Nuget site.
The issue I'm having is Resharper file analysis isn't enabled for this file (the file doesn't compile currently, I've left off a semicolon):
But, if I rename the file from "Test.cs" to "Test2.cs" then Resharper analyses the file correctly showing the syntax error:
Has anyone got any idea what's happening here? Is there a list somewhere of Nuget-supplied source files that are then excluded from Resharper analysis? The file properties are the same as for any other C# file, set to Build Action "Compile" etc.
EDIT1
To answer questions from Stephen below, this is Resharper 8, I'm not currently on 9. I've tried closing and re-opening the solution and excluding and re-including the file, neither of which help.
Interestingly, with analysis working correctly on the renamed "Test2.cs", if I then rename it back to "Test.cs" the analysis switches off again :S
EDIT2
Just to add some more info to this, if you Nuget-deploy C# files using the .pp extension, Resharper analysis works correctly on the resulting .cs files. Go figure.
ReSharper excludes source code that has been delivered as part of a NuGet package from analysis - it treats it as third party code that you did not write and do not want to maintain. E.g. it won't show any inspection results for files such as jquery.js or angular.js - you don't own these, don't intend to maintain them as part of your project, and any changes you do make are likely to get overridden the next time you update the project. So, inspections are disabled for these files, but the files are still indexed to allow navigation.
Just in case anyone else has this issue it only happens if Nuget delivers a .cs file. If you get Nuget to deliver a .cs.pp file, Resharper correctly analyses the resulting .cs file in your solution.
I was new to Nuget when I started this work and it turns out I needed to make them .cs.pp files anyway in order to use the correct project namespace when importing. So the problem has gone away.
I am getting Designer.cs file in my project and the comment in the file says it is being generated by an automatic tool.
This was an existing project so I don't know much about that. It is generated for one schema.cs which consists of schemas of all the tables in Database.
I am using a SQLIte DB.
Can any one help me in understanding what is the use of the Designer.cs file in a C# project.
There are lots of kinds of Designer.cs files in a Visual Studio project. Common ones are:
Properties\Resources.resx\Resources.Designer.cs file, the auto-generated code from adding resources to the Project + Resources tab
Properties\Settings.settings\Settings.Designer.cs file, the auto-generated code from adding settings to the Project + Settings tab
SomeForm.cs\SomeForm.Designer.cs, the auto-generated code produced by the Winforms designer from the controls you drop on a form
SomeData.xsd\SomeData.Designer.cs, the auto-generated code produced by the dataset designer.
Given that you name a database in your question it is somewhat likely that you are talking about the last one. You ought to see the pattern from the descriptions, you use a visual designer gadget in Visual Studio and it produces C# code that does something useful at runtime. You go back from the Designer.cs to the designer gadget by double-clicking its parent node in the Solution Explorer window. Don't ever edit the Designer.cs file, you'll lose everything when the designer re-generates the code. They are somewhat hidden in the Solution Explorer for that reason. If you haven't found them yet: open the nodes to see them.
Designer.cs contains the declaration and initialization of UI controls and layout of form. The form is rendered based on the information provided in designer.cs. This file is autogenerate when a form is created in design mode.
This is a pretty minor annoyance, but I noticed that every time I create a class in Visual Studio there are a few things that I would like to be automatically-generated for me. For instance, all of my classes start with a log4net declaration. It's only one line of code, but it's annoying having to find a class where I declared it in to copy/paste from.
In addition, there are multiple common 'usings' which I use, but that are not created by default.
Is it possible to setup VS to do this?
You just need to edit the default Visual Studio Class template. A walkthrough of the process can be found here.
From the File menu, select "Export Template".
You can create your own item/project templates. Here is a primer from MSDN:
http://msdn.microsoft.com/en-us/library/6db0hwky.aspx
Basically you'd create your own custom class template.
From MSDN:
Creating Project and Item Templates
How to: Manually Create Item Templates:
Create a project and project item.
Modify the project item until it is ready to be saved as a template.
As appropriate, modify the code file to indicate where parameter replacement should occur. For more information about parameter replacement, see How to: Substitute Parameters in a Template.
Create an XML file and save it by using a .vstemplate file name extension, in the same directory as your new item template.
Author the .vstemplate XML file to provide item template metadata. For more information, see the example in the following section.
Save the .vstemplate file and close it.
In Windows Explorer, select the files you want to include in your template, right-click the selection, click Send To, and then click Compressed (zipped) Folder. The files that you selected are compressed into a .zip file.
Copy the .zip file and paste it in the user item template location. In Windows Vista, the default directory is ..\Users\\Documents\Visual Studio 2010\Templates\ItemTemplates. For more information, see How to: Locate and Organize Project and Item Templates.
Although not exactly what you are asking for, one option is to declare code snippets. This will allow you to leave the standard class alone (not change the template) and add in the pieces that you require without having to find a class with it. However, if all your classes for every project you create requires the same basic set of code then Templates are the way to go.
C# Code Snippets
How to use them can be found How to Use Snippets
When building a C# application with Visual Studio 2008, is it possible to set a different output filename per configuration?
e.g.
MyApp_Debug.exe
MyApp_Release.exe
I tried a post-build step to rename the file by appending the current configuration, but that seems a scrappy approach. Plus it meant that Visual Studio could no longer find the file when pressing F5 to start debugging.
You can achieve this by editing your project file by hand. Locate the <AssemblyName> node and add a conditional attribute to it:
<AssemblyName Condition="'$(Configuration)'=='Debug'">MyApp_Debug.exe</AssemblyName>
<AssemblyName Condition="'$(Configuration)'=='Release'">MyApp_Release.exe</AssemblyName>
You'll have to duplicate it also to add another conditional attribute for the release version.
Whilst it is possible, it may cause problems. There is an AssemblyConfiguration attribute that can be applied to your assembly. In AssemblyInfo.cs, put:
#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif
This will add a property to your compiled assembly that will tell you which build configuration your application was built using.
As adrianbanks mentioned, you can edit your .csproj file by hand to accomplish this.
I would, however reccomend the simpler form of:
<AssemblyName>MyApp_$(Configuration).exe</AssemblyName>
If you ever edit the properties of this project however, this change will very likely be lost. It's something you will have to manually stay on top of, as it's not going to be a supported setup.
To manually edit your project definition, right click the project in Visual Studio, and select "Unload", then right click the unloaded project, and select "Edit" and it will open the XML definition for you.
I'm sure there is, however in my experience having different filenames for debug / release configurations is a bad idea as it can cause all sorts of problems (very much like the issue VS has when it tries to execute the renamed app)
Why not simply indicate whether or not its debug / release in the Assembly attributes (for example in the comments)