简体   繁体   中英

How do I find all namespaces in my Visual Studio solution?

I ask this question because I'm getting a compilation error in my solution: 'GiftCard' is a 'namespace' but is used like a 'type'

I tried adding a using directive like: using GiftCard.Data.Entity;

which is the correct namespace where GiftCard is found, but the error does not go away. When I add the fully qualified name in my code, ie GiftCard.Data.Entity.GiftCard

...then the error goes away, and code compiles.

But I'm wondering why the using directive does not work. I don't want to clutter my code with the fully qualified name every time I need to use the type. Somehow the error message is saying that I have GiftCard defined as a namespace somewhere. So how can I find all the namespaces in my solution so that I know where it is defined because I need to either remove or rename the GiftCard namespace.

Your have a different question and a different problem.

The Problem

The Framework Design Guidelines says Do not use the same name for a namespace and a type in that namespace .

For example, do not use Debug as a namespace name and then also provide a class named Debug in the same namespace. Several compilers require such types to be fully qualified.

That is:

namespace Debug
{
    public class Debug{ … }
}

OR

namespace MyContainers.List
{
    public class List { … }
}

Why is this badness? Do not name a class the same as its namespace

Answer for the Question

View -> Object Browser (Shortcut : Ctrl + Alt + J )

Use Powershell...

dir -r -filter *.cs | Select-String -pattern "^using" | Select-Object -expand Line -unique | Format-List -property Line

Run the above in a solutions root directory and you will the output something like...

using NUnit.Framework;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

for more info please go through https://social.msdn.microsoft.com/Forums/vstudio/en-US/03dae1a4-91ed-41eb-ae1a-082b56e42f67/how-to-get-all-namespaces-in-one-solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM