How to find C# files without a namespace in Visual Studio? - c#

It's come to light that at least one file within a subdir in my project had no namespace set, causing issues.
How can I find any other files missing a namespace?
I searched Google and Stack Overflow to no avail. I'd imagine it could be done with a RegEx Find in Files, but my RegEx is a little rusty.

Hi I have observed one straightforward thing that built in Visual studio itself. Open the solution in Visual studio and
Click on View > Object Browser
. Here It lists all the dlls and Projects. If you expand each project first it shows namespaces and inside them it shows sub items as Classes, Interfaces, Enums, Delegates ... . If any class doesn't have a namespace then it will be in Classes folder outside the namespace
Other way is
If your code is compiling fine, then you can check the compiled EXE or DLLs in the 'IL disassembler'.
To open the GUI, go to Start > Programs > Visual Studio 2013 > Visual Studio Tools > VS2013 x64 Native Tools Command Prompt and execute ildasm.
Classes without a namespace will be listed separately, and classes with a namespace will be listed in their respective groupings.

If you can get ReSharper, it adds an option in the right-click menu for projects (and possibly the solution) to adjust namespaces - that should pick up any classes that aren't in the correct namespace, based on the directories that each file is in.

Related

Adding Microsoft.DirectX.DirectInput To Visual Studio 2013 Express for Windows Desktop

I am trying to use some classes from the Microsoft.DirectX.DirectInput namespace, in the past if there was a namespace I couldn't use in my project I would just go to references and add the file with that name to my project, except that I cannot find the file in the list.
How can I use this namespace?
Found the answer at https://social.msdn.microsoft.com/Forums/en-US/2a08e16f-e05b-412e-b533-4b91f11db864/erorr-in-loading-directx-reference?forum=gametechnologiesdirectx101 Following the C:\Windows\Microsoft.NET\DirectX for Managed Code\1.0.2902.0 path, I was able to find the DLL I needed. Then just went to references > add reference > browse (the one at the bottom next to OK) then found the DLL.

How to Create a DLL file in Visual Studio C# 2010 Express edition?

I have already come across the Stack Overflow question "Is there a way to generate a DLL file from Visual Studio Express without explicitly creating a DLL project?", but it does not directly answer my query, so I'm raising it here.
The problem I am facing while trying to make the DLL is that I can't find any option under Build named build class file.
I have changed the project property to class file (shown below)
This is how it is:
And here is how my build option is getting displayed:
Also when I am using the command-line option the dll file is getting generated but it is not getting the properties I'm setting in the application.
I am new with Visual Studio so a litte bit confused about this part.
The "Build Solution" option in your second screenshot is the thing you need to click to produce your dll, alternatively you can right click on your project in the Solution Explorer and click "Build":
(If you only have one project in your solution then these two will both do exactly the same thing)
The output dll will normally be placed in the bin\Debug or bin\Release directory depending on whether you are in Release or Debug configuration, check the "Build" tab of the project properties for the exact path.
The reason why you aren't seeing a "Build class file" option is because this is what the "Build project" menu item does - it will produce a class library if the project output type is "Class Library", a windows executable if the project output type is "Windows Application" etc...
You're not trying to build a class file - you're trying to build a class library.
And you just build the solution - that will build each of the projects in your solution, including your LicenseCheckLibrary project is just a class library project.
It looks like you're basically there - look in the bin\Debug or bin\Release folders under LicenseCheckLibrary, and you'll find the DLL.
Why would you want to avoid building a DLL file in the first place? Are you developing an EXE file in order to test the logic and then conver it to DLL once it is working fine? If yes, why not create two projects: Windows Console and Class Library. Inside Class Library implement the licensing logic and use Windows COnsole to test the logic. When you say you are new with Visual Studio, what exactly do you mean? You never used it before or you are new to .NET Framework programming? .NET Framework has certain classes for developing licenses. Also, there were quetions here on stackoverflow regarding the licensing. Find some of them instead of reinventing the wheel.
Have a look at this article http://www.developer.com/net/net/article.php/3074001
Create a new class library project
Create classes and code
compile Project
Dll Created
Create a new project
Click on Add Reference
Navigate to the class library folder
Go into the debug folder or whatever and include
Remember you will prob have to include the namespace. in the new
project.

Is it possible to create 'invisible' project using Visual Studio addin?

I have an idea to create some sort of extended Immediate Window VS plugin. I've noticed that when I want to test something (like new Regex or DB reqest) I tend to create new console app for this. The idea is to create project that is not included in solution and references current project and has all using directives from current file. So I'll have all advantages of code editor: usings (no full class names), syntax highliting, IntelliSense, multiline commands, other plugins (R#/CR).
Is this possible?
Option A: Write those in a test-project added to your solution
Option B: Create a console application added to your solution
Create a build-script that only builds the projects you want to give to your customers.
To gain some ideas: look at http://mvcstarter.codeplex.com/ (point 9)

Refactor namespace in Visual Studio [duplicate]

I'm doing some architectural cleanup that involves moving a bunch of classes into different projects and/or namespaces. Currently I'm moving the files by hand, building, and then manually adding using Foo statements as needed to resolve compilation errors. Anyone know of a smarter way of doing this? (We're a CodeRush and Refactor! shop, but I'd be interested to hear if Resharper has support for this)
Visual Studio 2019 provides at least 2 built-in options:
'Move to namespace...' refactoring can be triggered on any class, and VS will prompt for the target namespace.
'Change namespace to...' refactoring is provided for when the current file namespace doesn't match with the folder structure.
This can be used to move individual classes to a different namespace by:
creating the desired folder structure
moving the file
applying the mentioned refactoring (CTRL+. with the cursor over the namespace)
These operation ensures that all references are updated accordingly.
Visual Studio 2010 has the possibility to rename a namespace. Place the cursor over the namespace name and press F2. Or simply rename it in the code and press Shift+Alt+F10, Enter after seeing the red squiggle appear.
Reharper can also rename namespaces. Quote:
The Rename Namespace refactoring
allows users to rename a specific
namespace and automatically correct
all references to the namespace in the
code. The following usages are
renamed:
Namespace statements
Using directives
Qualified names of types
As mentioned in the comments, this answer is now outdated. Please see the up-to-date answer below
Resharper is the only tool I am aware of what has this ability. There is also a lot of other functionality that it has that is missing in CodeRush and Refactor!
This answer applies to at least Visual Studio 2013 and 2015 with no resharper required
Move class files to new folder
Open 'Find and replace'
Select 'Replace in Files'
Type the original namespace definition in the 'Find what' field eg. MyCorp.AppStuff.Api
Type the new namespace definition in the 'Replace with' field eg. MyCorp.AppStuff.Api.Extensions
Select the new folder using the 'Look in' field's browse button ..., or type the folder path
Press the Replace All button
There are partial solutions for VS 2015 & VS 2017 without Resharper using free extensions.
One extension which I like today (end of 2017) is the Fix Namespace VS Extension:
https://marketplace.visualstudio.com/items?itemName=vs-publisher-599079.FixNamespace#overview
It analyses the folder structure of your solution and offers namespace refactoring using that. Unfortunately it isn't perfect: It doesn't track dependencies that well, but solved the lion's share of the work for me.
With Resharper: CTRL+R+O
Then press the down arrow key twice to select Move Type To Another Namespace.
Since the answer above was provided (I'm guessing) this feature has been added to CodeRush. Just place the carat on the Type to be moved and you'll see a Move Type to Namespace option on the Refactor! context menu. This will move the type to the new namespace and update references. You may still want to move the file to a solution folder that matches the name of the namespace though.
It's not the best outcome but can be done without plugins or tools, only with Visual Studio. Find and replace in Entire Solution, Match case, Match whole word.
Find what: class name, Replace with: New.Namespace.ClassName (fully qualified class name).
If you have 100+ references of the moved class and other classes in old namespace what are not moved this is the only foolproof and free solution I found. The only case when it leads to errors is when you have same class name in other namespace.
If you cannot, or do not want to use Re$harper, Notepad++ is your friend:
Make sure you don't have usaved changes inside Visual Studio for the files you need to move to the new namespace
Open all the files that contain the namespace that needs to be changed in Notepad++
Open Find & Replace (CTRL + H)
Fill the Find what and Replace with fields
Press Replace All in All Opened Documents
Save all changes in all documents (CTRL + SHIFT + S)
Switch to Visual Studio and reload all the documents (Yes to all at the prompt)
DONE

Support for VB.NET's Imported Namespaces feature in C#

I am use to VB.NET. The game source code I am learning from is written in C#. I find it annoying that I have to add using System.Diagnostics to the source code in order to type Debug.WriteLine.... I checked under project properties, but I cannot find the References tab that allows me to add namespaces to Imported Namespaces. Where do I find this in C#?
Also, why can't I do this in C#? Imports System.Math
Place the cursor over Debug in the source code, a red squiggle appears in the right bottom corner of the word, press Shift+Alt+F10 Enter - the using is automatically added.
Also, why can't I do this in C#?
Imports x = System.Math
You can: using x = System.Math;
I don't think you can have "hidden" namespaces in C# like you can in VB.NET (not sure).
As for the second part about System.Math, you can do the following at the top of each file.
using SM = System.Math;
SM.Abs(...);
It is possible to modify Visual Studio's template for new C# classes. This is not exactly the same feature as in Visual Basic, but for any newly created class you can get the namespaces that you like.
It's a little more than just a few mouse clicks unfortunately, but you will find all the details described in Anson Horton's blog post:
Item templates - adding references by default
Note that this allows you not only to modify the default using directives but also to modify the assemblies that get referenced automatically when adding a new class.
As the blog post related to Visual Studio 2005, you probably need to adjust some paths, e.g. the class.zip file is located under C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033 in Visual Studio 2008.
In c# you should always explicitly specify namespaces you want to use or use a full name:
System.Diagnostics.Debug.WriteLine ( ... );
Also, there is a references tab under a solution view, you have to reference desired assemblies there, because many assemblies are not referenced by default.

Categories