Removing part of a namespace across entire project - c#

I know that I can rename a namespace using Visual Studio 2010, but I need to remove part of a namespace.
namespace Xyz.Common.Utils { ... }
Renamed to
namespace Common.Utils { ... }
I need to drop the Xyz part, but don't see a way to use the VS refactoring tool to do this.

This is what I did a few times and you don't need any third-party tool:
Refactor the name in to something unique like: fjhfhchdbyegdrkoksodbc (if that's unique enough to you)Then do a global replace on fjhfhchdbyegdrkoksodbc. (including the dot) with an empty string.

There's no way to do that in pure Visual Studio, although there's an option when you select a word with the right button, then Refactor »» Rename in VS2010, this option requires you to select a symbol. In your case
namespace Xyz.Common.Utils { ... }
Xyz would be the symbol to refactor, but it cannot be renamed to an empty value.
Telerik JustCode can do the job instead:
http://www.telerik.com/products/justcode/features.aspx
There's also a Trial Version.

Related

Visual Studio auto generated code from gRPC .proto file

I'm facing an issue where it seems that Visual Studio is changing the way objects look in a .proto file.
.proto file: concordium_p2p_rpc.proto
Example - this is in the .proto file:
message BlockHash {
string block_hash = 1;
}
After building my project I want to create an instance of this exact object, however, the property inside the object is now called "BlockHash_" and not "block_hash" as in the .proto file.
The confusion is complete if I try to serialize this object to JSON... Then the attribute is now called "blochHash"... a 3rd name.
So in .proto
{
"block_hash": ""
}
becomes in C#
BlockHash bh = new BlockHash()
{
BlockHash_: ""
}
and then when serialized
{
"blockHash": ""
}
What is with that?
OK, so I found the answer to this question myself in this article.
https://visualstudiomagazine.com/articles/2020/01/06/defining-grpc-messages.aspx
Why Microsoft decides to perform this renaming instead of just keeping the names are as they are in the .proto file, is beyond me and evidently it introduces problems that I don't see would have been there otherwise.
What happens is that Visual Studio wants to be the boss of what things are called, and therefore it wants to rename the attribute "block_hash" of the object BlockHash by making the first letter upper case, removing the underscore and making the first letter of the next word an upper case as well => "BlockHash".
However, since this is the name of the class itself, this name is probably problematic since class constructors use this name and therefore Visual Studio fixes it's own dodo be adding a tailing underscore, resulting in an attribute named "BlockHash_".
Why oh why do you do these things?!....

Class always tries to open in Design View, not Code View

I have a class named Print.cs that always stubbornly opens in Design View, when I need to see its Code View (F7). It also has a different icon to the rest of my classes in the Solution Explorer.
I've looked in the Properties and can't see anything relevant. I've also tried deleting and re-creating the class, but the icon comes back.
How can I force Print.cs to always open in Code View?
(Click to enlarge)
NB: disregard the green squiggly line, it's just a warning that unreachable code was detected.
Taken from the suggestions from #LarsTech and #OrkunBekar, since neither posted this as an answer - this works:
[System.ComponentModel.DesignerCategory("Code")]
Added between the namespace and the class, e.g.
using System;
using System.Collections.Generic;
...
namespace POS
{
[System.ComponentModel.DesignerCategory("Code")]
public class Print : PrintDocument
{
...
}
}
Try right click on the file -> Open With -> CSharp Editor (remember to set it as default).
Funny enough, it was totally other thing in my case.
If the filename equal (=) to the first class in it, then Visual Studio decides it is a simple C# file. If you have 2 classes and the first is not equal to the file name, then the solution icon changes and default editor is designer.
I don't know if you have same conditions in that link but opening your class in notepad, changing codes, replacing file then building the solution again may fix the problem.

Is there a way to instruct visual studio to auto add fields at the bottom of the class instead of the top?

namespace Guilds
{
public class Wizard
{
public void Wear(IClothing clothing)
{
Console.WriteLine("Puts on the {Robe} and {WizardHat}".Fmt(clothing));
}
IClothing _clothes;
IWeapon _weapon; // <== I want my fields added at the bottom of the class!
}
}
I am aware that if you put your fields at the bottom, it will start adding subsequent ones to the bottom of the class as well. I would love to have this as the default behavior even for the first field.
This behavior is usually triggered when pressing Ctrl + . on top of an undeclared field.
Use Regionerate and create your own format template. it's free tool to use with visual studio.
Edit: You can also use CodeMaid because it seems that Regionerate and VS2012 do not work together (I have not tested that combination at all though. I have VS2010)
Edit Adding more to my previous reply. CodeMaid is really cool and you can specify the layout in configuration. Also in configuration, you can specify that file should be formatted on save. This way write your code in anyway you want and have it formatted when you press Ctrl+S! I am one happy user of CodeMaid. Also I am using Visual Studio 2013.

Visual Studio 2008 custom class item template, $safeprojectname$ not reconciling

Just setting up some quick class and object item templates and all is working great so far, but one thing I'm stuck on is the $safeprojectname$ template parameter.
I've added this as part of the namespace portion and the $registeredorganization$ is working fine
namespace $registeredorganization$.$safeprojectname$
{
public class $safeitemname$
{
public $safeitemname$()
{
//default constructor
}
}
}
And I've gone into the .vstemplate file and made sure ReplaceParameters="true" so the only thing I guessed at this point is the period between the company and project name, so I tested this out and just for laughs removed the period, still no go. Anyone have any insight as to why this isn't working?
EDIT: I'll accept Jared's answer as it answers my core question "why isn't it working?" but I am adding this follow up to show how I got my desired result. Instead of using the $safeprojectname$ or $projectname$ I found that using $rootnamespace$
namespace $registeredorganization$.$rootnamespace$
or depending on how your project is named:
namespace $rootnamespace$
works as I had wanted, just added this for anyone else who may come across this issue.
The $safeprojectname$ template replacement macro is only available from the New Project Dialog. It will not work for anything added as in individual item.
Reference: http://msdn.microsoft.com/en-us/library/eehb4faa(VS.80).aspx

Alias and namespace conflict in Visual Studio Designer

I have a namespace conflict between two referenced assemblies:
i.e., I'm referencing Foo.A.Foo and Foo.Bar, so when I say I want Foo.Bar.Control, VS is trying to find Foo.A.Foo.Bar.Control
I can twiddle the Designer.cs code by adding new global:Foo.Bar.Control(), but as soon as I change anything, VS switches back.
I know there's something about adding aliases directly to the reference, I've tried but haven't managed to find the right combination (inline alias, using alias, reference alias).
Help?
"extern alias" may be what you mean, but I'm not sure what the designer will do with it, unfortunately...
I'm not even sure that's what you're after though - that's normally for two types from different assemblies with the same name.
You can write namespace aliases with a using directive, e.g.
using FooControl = Foo.Bar.Control;
but again, the designer is going to rewrite your code...
OK, this isn't the answer, but it's what I found for a workaround:
namespace FooBar
{
class FooBarControlHack : Foo.Bar.Control { }
}
So I can do the following in the Designer.cs :
this.fooBarControl = new FooBar.FoorBarControlHack();

Categories