visual studio 2010 inserts using directives inside namespace - visual-studio-2010

I have exactly opposite problem to one described here. In my case Visual Studio inserts using directives inside namespace and I want to prevent this. I did try to uncheck Resharper option:
Languages -> C# -> Formatting Style -> Namespace Imports -> Add using directive to the deepest scope
And it didn't help. Also I tried to temporary disable the Resharper. Still same issue.
Btw, I have StyleCop and StyleCop+ installed as well. Maybe it is causing the issue.
So right now when I go and Add New Item -> Class - it will create new code file with using directives inside namespace. How to change this?

Have you replace your class template file with one that has one or more using directives inside the namespace declaration? If so, you're probably seeing the result of an interesting bit of C# plugin behavior: a newly added using directive is placed after the last recognized using directly already in the file, regardless of where that is.

Related

Adding usings inside the namespace

Is there a way to configure ReSharper to add using references to a file within the appropriate namespace?
Currently it adds them to the top of the file, which is fine, until you want multiple namespaces in the same file.
For example, I would like it to be like the following:
namespace A
{
using System;
// other classes
}
Options > Code Editing > C# > Namespace Imports:
Insert using directives when necessary > Add using directive to the deepest scope.
Just to bring an up to date answer here as I was wanting to do this but not having R# (ReSharper) installed...
Its now available from Visual Studio 2019 directly since the 16.1 Release.
I found this on developercommunity.visualstudio.com
The code style to include using directives inside or outside a namespace was added in Visual Studio 2019 update 16.1. You can now set your preference in Tools > Options > Text Editor > C# > Code Style or in an EditorConfig file and the IDE can apply it through messages, warnings, or errors depending on the severity you specify in the code style rule

Dim/hide logging lines of code in Visual Studio

Is there any way to make visual studio dim or hide/show on demand logging lines of my code?
We use a lot of logging in our project and it's harder to read code like this.
I would like it to be like this, for example:
Unobtrusive Code extension worked for me for Visual Studio 2019. It dims the opacity of log lines (and comments, which I disabled - I enjoy reading my comments). He did a quick update for the nuget package, and it works great.
https://marketplace.visualstudio.com/items?itemName=niklaskallander.UnobtrusiveCode
I use this. Hoping one day they will add color customization and line selection regex options as well:
https://marketplace.visualstudio.com/items?itemName=ElmarXCV.GrayLogLines
There is no way to do this from the standard Visual Studio IDE. In order to do this you would need to define a custom extension which recognized lines like this, tagged them with a specific format and have that format be colored a lighter color in the IDE
a "hackier" way would be to wrap all logging in a preprocessor directive like
#if DEBUG
Log.Info(........)
#endif
Visual-Studio will "dim" the code inside.
and have some kind of config header where you
#define DEBUG 0
Not the prettiest but its nice if you don't want debug code compiled into your Release binary
why don't you put your section within #region tag.
E.G:
#region Put some region name here for your reference
Your Code / Comment / Whatever
#endregion

Finding T4 text template class code

T4 text templates can be used to generate not only code but also any kind of text with visual studio.
I've read blogs and tutorials about T4 and as far as I can understand, visual studio dynamically builds a class in the background, compiles and runs the code in that class to build the text output.
Is it possible to see the source code of that class?
Yes, the easiest way is to change the Custom Tool in the properties window when the template file is selected in Solution Explorer.
By default, it will be 'TextTemplatingFileGenerator'.
If you change the custom tool to 'TextTemplatingFilePreprocessor' you'll get the underlying template class instead of the template output generated into your project.
To be precise, this code won't be exactly the same as that which is run under the covers, but it will be very close.
If you need the absolute exact code, you should leave the custom tool alone, but set the debug="true" flag on your <## template #> directive. This will then leave the generated code sitting around in a random named '.cs' or 'vb' file in your %TEMP% directory. Just sort the directory by time and it should be up at the top.

Can I use CSS file extension instead of cshtml for the Razor view?

The idea is that I'm building a CSS file generating service, it's working just fine. But I need the view file extension to be CSS instead of cshtml so I take advantage of the visual studio intellisense.
Any ideas?
The only way I can possibly think this setup a custom HTTP handler; there you can give it an explicit name, and try giving it a file extension of CSS... Not 100% sure it will work, but you could give it a shot; though you would need to move the code to a class implementing IHttpHandler.
However, I don't think either way will work with Visual Studio Intellisense. If you want intellisense, add the CSS to the markup, then remove it programmatically at runtime. This is one possible way to work around it.
You could create your own ViewEngine which inherits from the Razor View Engine. There you'll need to set the FileExtensions property to include css. Here is a guide to creating it. The other option is to find the Razor View Engine from the View Engines list and try setting it there.

Automate refactor import/using directives, using ReSharper and Visual Studio 2010

I want to automate the Visual Studio 2010 / Resharper 5 auto inserting import directives to put my internal namespaces into the namespace sphere. Like this:
using System;
using System.Collections.Generic;
using System.Linq;
using StructureMap;
using MyProject.Core; // <--- Move inside.
using MyProject.Core.Common; // <--- Move inside.
namespace MyProject.DependencyResolution
{
using Core;
using Core.Common; // <--- My internal namespaces to be here!
public class DependencyRegistrar
{
...........
}
}
Currently, I'm doing it manually, the problem is that with every refactoring the using directives going up, to the beginning of the page.
In R# 5.0:
ReSharper->Tools->Cleanup Code. Or simply press Ctrl+E, Ctrl+C.
Then use profile that has "Optimize 'using' directives" turned on.
I think readability is better served if the statements are either (all) outside the namespace declaration, or (all) inside of it.
Among the using statements, sorting them with the project statements last (as per your example code) is then preferred.
Resharper follows both the above conventions, so I would recommend sticking to those :)
There is no option to achieve that. So probably the best action go with is a convention that you can achieve easily.

Resources