Does anybody know how one can change the namespace of a "Coded UI Test Map" also known as UIMap?
Or how the designer does the naming?
When adding a UIMap to your project, you get to cs-files:
e.g.
UIMap.cs
UIMap.Designer.cs
The designer file is auto generated (thus not for editing yourself) and the other file is for own customizations.
In my case I want to change the namespace of both files because I added some folders to organise stuff.
What I did was manually changing the namespace of both files. But since the designer generates file 2) the namespace was also changed again, screwing up my references.
Now I hoped to find a property to enter my own namespace naming. The property I found was for the file "UIMap.uitest", named "Custom Tool Namespace". This property didn't do the trick.
I also had a look in the xml of "UIMap.uitest" to find a namespace reference but again no success.
So I guess the naming is hardcoded by the designer...
The designer always generates this namespace:
namespace TestProject.UIMaps.UIMapClasses
"TestProject" is the default sln namespace
"UIMaps" is my added folder
"UIMapClass": don't no the origin but seem to be auto-generated from the name of the "Coded UI Test Map" (here UIMap) + "Classes".
Can anybody confirm the namespace naming convention by the designer? Or knows a way to manually fix it?
If you are talking about the root namespace, it's in the project properties dialog
right click the project
select the properties menu item
on the application tab, look for the Default namespace: text box and put what you want in there. It can consist of several levels
e.g.
BigNamespace.CompanyNamespace.ITProject.Solution.Project..... etc.
when you perform any action that will require a rewrite of the file, those namespace lines get rewritten. Or you can do it by hand.
I looked it up because it was giving me heartburn too.
Related
I have a C# project and I am mapping a lot of classes from Json models.
Some of the names are colliding so I have to setup namespaces every time. I generate the classes with an online tool to create C# classes from JSON, so by default the classes don't have a namespace.
Is there any way, with right click on the class or some tool on Visual Studio 2019, that allows me to automaticaly select a bunch of classes and set a namespace for them? Or right click a class and set it's namespace as it's path in the filesystem from the workspace folder?
The problem is that right now, my only chocie is copying the namespace, pasting it on every class and surrounding the code with brackets again and again. I just want to make this proces easier.
Also I know it's not needed to place every single class in a namespace, but to prevent execution time errors and keep the code sorted, I prefer to do so.
You can Paste JSON As Classes in Visual Studio.
It will create the classes with the current namespace. Simply create a new class, "Paste JSON As Classes" and it will create every model from the JSON on the clipboard.
You can user Resharper to adjust namespaces of existing classes.
https://www.jetbrains.com/help/resharper/Fix_inconsistent_namespace_naming.html
# Francesc Bosch.
For changing the namespace of an existing class, you can right click on your current namespace and select Rename -> change to your new namespace-> click Apply.
If you have multiple depths of the namespace, Visual Studio will not allow you to type dots. However, if you copy and paste a dot, it will succeed despite the warning.
You could also move the class to the changed target namespace.
1.Right-click the name of the class you want to move -> select Quick Actions and Refactorings... -> click Move to namespace...
2.In the dialog box that opens, select the target namespace you'd like to move the type to.
I have been implementing this solution explained here to make an outlinedTextbox.
I have created a test project and added directly in the main namespace and it works.
Now I want to add it to a library (HelperLib) and want to use it in whatever program of mine I want. For example here the program is called pcdLoggerS2.
but when I add it to my xaml it says that
so it's a matter of namespaces.
Therefore I have add this
xmlns:local="clr-HelperLib"
in my definition of my Base:WindowViewBase but nothing has changed but it's really in that namespace!
--ADD FOR DAVID---
You need to make sure that you've added the HelperLib project as a reference to your test project.
Open PcdLogger and right click on References, and add that project as a reference. Until you do this, the XAML in your test project will not be able to find the correct assembly.
Additionally, when you reference a namespace, from another assembly, you need to add that information to the namespace declaration in your XAML.
(See: https://msdn.microsoft.com/en-us/library/bb514546%28v=vs.90%29.aspx)
As an example:
(I would suggest leaving local for your local namespaces)
xmlns:helper="clr-namespace:HelperLib;assembly=HelperLib"
EDIT:
Additionally, Visual Studio's intellisense is fond of telling you that the namespace, or class, does not exist until you have built the project. You may have to rebuild and/or close/reopen the xaml file for intellisense to cooperate again.
check Namespace of you class.
HelperLib its name of a class, but not namespace.
Try to write this:
xmlns:local="clr-namespace:HelperLib"
I get this strange thing when I create a new class, Next to the namespace name I get a .Classes which all I can tell prevents me from creating an instance of the class and hides it.
I would include the screen shot but im not sure how I do this. Please help so you can better understand the question.
Thanks
The namespace within Visual Studio will depend on two things:
The default namespace defined within the project properties.
The folder structure within your project.
I think that within your project you have a folder Classes where you put (well as the name guesses) your classes. Due to this fact Visual Studio will automatically append this name to the default namespace.
So either manually change the namespace manually right after you added a new class or move the file one level up within your folder structure of the project.
I hit this weird namespace issue when adding my first 'Service Reference' to a client project in Visual Studio 2010.
If my project's default namespace uses two or more parts, e.g. MyCompany.MyApp then when adding a Service Reference a Reference.cs file is created containing the namespace MyCompany.MyApp.ServiceReferenceName with a lot of auto-gen code with fully qualified names, e.g. System.SerializableAttribute, System.Runtime.Serialization.DataContractAttribute.
The Reference.cs file will be full of compilation errors because the compiler starts treating the System namespace as sub member of the MyCompany.MyApp namespace. You get an awful lot of errors along the lines of:
The type or namespace name 'Runtime' does not exist in the namespace 'MyCompany.MyApp.System'...
If I amend the namespace at the top of the Reference.cs file to something simple, e.g. MyCompanyMyApp.ServiceRefernceName then the compiler behaves and recognises the System namespace references as decleration of .net's System namespace.
I'm using a different workaround for now as I really want to keep my multi-part namespaces. My current alternative is to append global:: in front of the System namespace references to force the complier to do the right thing. In fact, if the 'Add Service Reference' wizard uses T4 templates I may just amend those to embed my workaround at the source.
Questions
I'd really like to understand what's going on here and why a multi-part namespace causes this issue. Presumably there's more to namespaces than I thought. Secondly, would really like to work out a better solution than performing a global Find/Replace every time I add a Service Reference or mucking around with some T4 templates.
I found the answer here somewhat unclear, so I thought I would add this as an example (I would do it in the comments but it looks better here):
So I have this as my default namespace:
namespace RelatedData.Loader
But I also add a class named:
public class RelatedData
{
}
Because the class name matches a portion of the namespace when it generates your proxy with Add Service Reference it gets confused.
The answer here was to rename my class:
public class RelatedDataItem
Ahh well I found the cause eventually.
I'm working against a very large third party WCF API and ... one of their namespaces is LameCompany.System (!!) Carnage then ensues...
Arrrgghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
The lesson to learn here is when Visual Studio/.net compiler stops recognising the BCL's System namespace you have a namespace/type in your project called System. Find it, remove it, shoot the developer that created it.
I found that having a class name similar to your namespace causes this.
Try renaming your class name
I ran into a similar issue with VS2012 described by jabu.hlong and Simon Needham after minor changes in the client project that has the references to the WCF services after updating the reference to the services. I got lots of errors compiling the Reference.cs files generated and so on (the generated files of the XAML as well).
I have selected to reuse types from specific assemblies in my solution and got a similar problems with the namespaces.
The error I get is that the namespace of the reused assembly and the namespace of the generated types can not be found when used in the Reference.cs. Both namespaces have in common the first parts, as they are from the same solution. My namespaces in the solution are like appname.tier.technology.project. Both conflicting namespaces are Appname.Dto.Modulename (the reused assembly) and Appname.Client.Wpf.ServiceName (the namespace in the client project using the services for the generated types).
The problem arises after a minor change in the client project, when I created a new utility class in the namespace Appname.Client.Wpf.Appname. I choose that namespace because the Appname is also the name of a module in the client project. This seems to confuse the compiler and can not resolve both namespaces in the generated Reference.cs. After changing the namespace of the utility class to avoid using two identical parts in it and updating the service reference, the compiler errors in Reference.cs dissapears.
I tried different things (and tried different namespaces when adding the service reference), but nothing worked for me except this brute force fix - in my case it was OK but I am aware it's ugly (and needs to be repeated if you use "Update Reference" in the future):
Since the WCF service namespace is added to your default namespace, just search and replace all mentions of the newly added
MyNamespace.ServiceNamespace
with
ServiceNamespace
in the whole solution (use your own namespaces of course), including the auto-generated Reference.cs file.
Basically, the problem is a name conflict where one name is hiding another. A folder or class named "System" can do that, but if you also have a class with the same name as your project, you'll see the same thing. Sure, you can rename everything in the reference.cs, but it's probably better to rename your conflicting class.
I had folder in my project called "System" (yes, very stupid of me) and that caused some issues in the references.cs.
Renaming the folder (and the namespace), fixed the issue.
Here is how I solve this issue on VisualStudio 2017 trying to add a reference to a web service in a test project.
After trying adding the references, rebuilding, closing, reopening and spending some time on the issue, I noticed that VS had put the files it creates to reference the WS in a folder named "Connected Services".
I renamed the folder without the space then opened all the files in the folder and the csproj with a text editor, replaced all the occurrences of "Connected Services" to "ConnectedServices" and reopened the project.
I then added references to System.Runtime.Serialization and System.ServiceModel and everything now works fine.
This is a bug in Visual Studio (still is at version 2022). To fix, remove the namespace in the reference.cs file. So if your namespace is "myapplication" and your service is "myservice", you'll see myapplication.myservice in the reference.cs file. just delete "myapplication." everywhere and make sure it isn't auto-generated again (lest you have to re-delete everything).
First, a little background.
Currently namespaces and assemblies in our codebase (~60 assemblies, thousands of classes) looks like
WidgetCompany.Department.Something
We have now been spun off such that we are selling the software that drives a WidgetCompany, so we 'd like to rename the namespaces & assemblies
NewCompany.Something
Under normal circumstances I'd probably just stick with the old namespace, but the problem is our customers don't want to see the name of one of their competitors anywhere in the application. In the off chance that they see a stack trace, assembly properties etc, it shouldn't show up. It's not meant to hide our associates or do anything sinister, we just need to make sure that people know we are a separate entity, separate management, and they don't need to worry about data being shared etc.
Now the question. What is the best way to perform this type of all encompassing rename?
The following would need to change:
Namespace for (almost) every class in
the application
Every using statement in the application which references the old names
Folder structure for each project
References between projects which rely on changed folder structure
.Sln files which reference the changed folder structure
Any references to
those classes which are fully
qualified (should be few and far
between)
Any references to those
classes in xml config files (config
sections etc)
AssemblyInfo.cs files for every assembly
AssemblyName in every .csproj file
Am I stuck with the find-replace-pray strategy or is there something better?
Right click on your current namespace and select Refactor -> Rename and change the name in the pop up that comes up after a while. Enter your new name and click ok.
If you have multiple depths to your namespace, then Visual Studio won't let you type a dot. However, if you copy and paste a dot, despite a warning, it will do the business.
To completely change to the new name, you will likely need to make additional changes manually. You can find where by performing a project search (ctrl+shift+f) for other references to the name in the Project, Solution, or other supporting files from a separate text editor like VS Code. Finally, folders may need to be changed manually as well.
ReSharper. Get version 5.1 from JetBrains for free for 30 days (more than enough time to do your renaming). Put the cursor on the namespace you want to change and hit Ctrl-R twice (ReSharper Rename). It'll work with a namespace any number of levels deep, and converts any usage of that namespace to the new one. However, you will have to change every unique namespace in your solution (unless you just go with Find/Replace)
Visual Studio 2019 Community Edition supports this as described here. It works for any hierarchy (with dots, root namespace changes etc) and correctly updates all dependencies.
Place your cursor in the class name
Press Ctrl+. to trigger the Quick Actions and Refactorings menu
Select Move to namespace
In the dialog box that opens, select the target namespace you'd like to move the type to
If you have ReSharper:
Right click project, Properties. Change Default namespace to desired
namespace.
Right click project, Refactor -> Adjust Namespace to update all
the namespaces to use the default namespace
Then just let it do its magic.
Firstly I would try Refactor->Rename option. But as mentioned in comment for another answers it doesn't work that good (Or I haven't found how to make it working). So I prefer using following scenario, especially if you want to add some addiotional namespace.
1) Rename your root namespace (WidgetCompany) to something like NAMESPACE_TO_BE_REPLACED using Refactor-Rename
2) Replace NAMESPACE_TO_BE_REPLACED with your final namespace (NewCompany.WidgetSoftware) using find-n-replace dialog
And do not forget to rename projects, default namespaces, etc.
Open a random class in [WidgetCompany.Department.Something]
Edit->Find and Replace->Replace in Files
Type "WidgetCompany.Department.Something" in FindWhat area
Type "NewCompany.Something" in Replace with area
Select Current Project in Look in area.
Replace All
Alternative solution if you've already partially renamed:
Open find and replace (shortcut ctrl + h)
Make sure regex is selected (icon like .*)
Paste the full name of your current namespace, for example YourSolution.YourProject
Paste this regex YourSolution.Your[partiallyRenamedNamespaceCharacters].*
Make sure Entire Solution is selected not Current Document
Run
For Visual Studio 2022 this has finally been solved:
Right-click on the project or solution (not the directory or file)
Select "Sync Namespaces"
The namespaces in your entire project should now reflect the folder structure.