I am trying to add a custom namespace to my permanent library so that I can us it any future project. I want to just add "using MyNameSpace;" to be able to access it whenever needed. How would I go about doing this, if at all possible? I have looked every place I could think to look, but have come up with nothing. Please help.
You add
namespace MyNameSpace {
after all global using or import/include directives, in all files. Then add the terminating
}
at the end.
Do this for all source/header/etc. files.
Related
I am using Visual Stuido 2022 to code my C# project.
Is there a way to configure VS using (.editorconfig file) where a new line is added before and after the namespace?
So my class will look like this
using System;
namespace ProjectName.Tests;
public class Test
{
}
instead of
using System;
namespace ProjectName.Tests;
public class Test
{
}
I'm not sure there is a Visual Studio native way of doing this.
There is definitely not a way to do this in .editorconfig with Visual Studio alone (meaning no plugins). About halfway down Namespace declaration preferences, it talks about csharp_style_namespace_declarations, and the code formatting sample when that value is file_scoped looks like
// csharp_style_namespace_declarations = file_scoped
using System;
namespace Convention;
class C
{
}
which appears to get you part of the way there (blank line after using). When you look at the supported formatting rules, the list is pretty brief.
If you have ReSharper there is a way. These settings in .editorconfig will do what you want:
resharper_blank_lines_after_file_scoped_namespace_directive = 1
resharper_blank_lines_after_imports = 1
If ReSharper is not an option, here are 3 possible paths to take, none all that great. They certainly aren't simple solutions.
try to find something in the Visual Studio Marketplace
write a .NET Analyzer that is configured via .editorconfig (ref. this page)
raise an issue on Developer Community, and hope they get to it.
Like the other guy said,
No, not with editor config (out of the box).
Resharper can do this; you can build a custom format for your code and tell it, for example, where you want certain sections like imports below the class for some weird reason ;) ...of course, with that spacing around those sections.
But now you have to get ReSharper licenses for all your devs. The Resharper devs are very cool, and Jetbrains has been building quality software for a few decades. Maybe I'm sentimental.
Well, there is another option if you don't want to pay...
In DevOps roles of the past, I've used Roslyn to build extensions for custom formatting.
Build a custom Rosyln analyzer.
https://learn.microsoft.com/en-us/visualstudio/code-quality/roslyn-analyzers-overview?view=vs-2022#severity-levels-of-analyzers
You can build a custom code suggestion with this of configurable severity.
It's pretty easy, too, thanks to Rosyln, but you must think a little backward. Let me know if I can help more.
StyleCop.Analyzers has a bunch of formatting related rules that you can enforce and, in some cases, automatically fix across your codebase.
Unfortunately it looks as though there is not yet support for file-scoped namespace formatting in the way you want. There's a PR to add it here, so if you really wanted to have this you could get that PR across the finish line, or make your own build:
https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512
This issue might also be worth a look:
https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3578
I've been practicing Caliburn.micro with mvvm pattern.. till I noticed that I have named the folder"ViewModels" ModelViews which in regards to caliburn doesn't work unless you specify the standard names, but after renaming it I noticed it that visual studio still uses the old path for example when I try to call the class LoginViewModel it automatically calls this path:
using Login2.ModelViews;
event if I try to manually type using Login2.ViewModels;
it doesn't recognize it.
Any suggestion would be appreciated I don't want to restart my project.
I just solved the problem by naming the namespace of the ViewModel classes
from namespace Login2.ModelViews to namespace Login2.ViewModels hopes this will help someone in the future ^-^
First off here is my file structure
Services.Abstraction.Common
Services.Abstraction.Claims
Services.Abstraction.Clearance
In Visiual Studio 2010, I am in the Claims project and I want to access classes from the Common layer which is on the same level as both Claims and Clearance. I use the namespace below in the Claims project. However it is saying the Common doesn't exists and all the functions that are in the Common folder also don't exist.
using System.Collections.Generic;
using System;
using System.Xml;
using System.Xml.Serialization;
using Services.Abstraction.Common;
How do I fix this? Do I have it wrong in the namespace or do I have to add something to my assembly?
http://msdn.microsoft.com/en-us/library/7314433t(v=vs.80).aspx
There is not much to say here... this link explain you how to add a reference in visual studio, probably you problem is that you are missing a reference to the other dll or project, am I wrong?
I'm using my own Extension Methods throughout several of my projects. When I compile(Build/Rebuild) all is fine. However when I start the debugger and the code recompiles, I get a list of errors all associated with the extension methods stating that they aren't a member of the class they are attached to. Any thoughts on why this might be happening? It seems intermittent as I can recompile and clean and recompile several times and it finally will allow me to launch the debugger.
Possibly the PDB's are not being generated properly? Are you launching from VS or attaching to a process? If attaching, make sure the PDB's are in the same location as the application.
Try:
Project Properties -> Build -> Advanced
Set 'Debug Info' = Full
Also, what namespace do you have the extension methods in? Make sure they are in the ROOT namespace or just remove the namespace all together (just for now as a test).
What kind of types are you extended? Is it possible you have two types of the same name and you're trying to extend the wrong one?
I am using VS2010, is there a way, to disable full root namespaces, when VS is autogenerating code? From this:
{
global::System.ComponentModel.CollectionChangeEventHandler schemaChangedHandler = new global::System.ComponentModel.CollectionChangeEventHandler(this.SchemaChanged);
}
to this:
using System.ComponentModel;
{
CollectionChangeEventHandler schemaChangedHandler = new CollectionChangeEventHandler(this.SchemaChanged);
}
I don't know if there is, however doing so is a VERY BAD idea.
VERY BAD.
VERY BAD.
VERY VERY BAD.
First, it suggests you are editing generated code. If you are, see above. The solution is to use partial classes, if you aren't already. Almost all generated code is done using partial classes. If not, open a Connect.
Second, it is there for a reason--it prevents generated code class names and namespaces from clashing with yours. When generated code isn't globally scoped and it does clash, you only have two choices: Either rename your code or edit the generated code every time you regenerate it. This is the definition of a Pain in the Ass.
I know it doesn't look pretty, but the fact is you should NEVER be looking at it.
NEVER.
etc.
Here's a connect I opened because the EF4 T4 templates didn't globally scope their variables, so the generated code clashed with my Debug namespace.