Question How do I get the namespace of my .NET 7 application

decuser

New member
Joined
Nov 28, 2022
Messages
2
Programming Experience
10+
I asked this on SO and got skewered. I love SO for the answers, but asking a question is fraught with peril. I'm new here, but I'm hopeful y'all will be more forgiving and helpful.

if I:
create a new 3.1 app:
dotnet new console -f netcoreapp3.1 -n four

add to Program.cs
Console.WriteLine("Namespace: {0}",
    MethodBase.GetCurrentMethod()?.DeclaringType?.Namespace ?? string.Empty);
Console.WriteLine("Class: {0}",
    MethodBase.GetCurrentMethod()?.DeclaringType?.Name ?? string.Empty);
Console.WriteLine("Method: {0}",
    MethodBase.GetCurrentMethod()?.Name ?? string.Empty);

and then dotnet run, I see:

Namespace: four
Class: Program
Method: Main

But if I:
create a new 7 app:
dotnet new console -f net7.0 -n seven

add to top:
using System.Reflection;

Console.WriteLine("Namespace: {0}",
    MethodBase.GetCurrentMethod()?.DeclaringType?.Namespace ?? string.Empty);
Console.WriteLine("Class: {0}",
    MethodBase.GetCurrentMethod()?.DeclaringType?.Name ?? string.Empty);
Console.WriteLine("Method: {0}",
    MethodBase.GetCurrentMethod()?.Name ?? string.Empty);

and then dotnet run, I see:
Namespace:
Class: Program
Method: <Main>$

Is the code really running outside of a namespace? or is there another way to get the active namespace?

Thanks!
 
Solution
That's because with your four project, you added the extra code to the existing template, so you effectively have:
Program.cs in four:
using System;
using System.Reflection;

namespace four
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine("Namespace: {0}",
                MethodBase.GetCurrentMethod()?.DeclaringType?.Namespace ?? string.Empty);
            Console.WriteLine("Class: {0}",
                MethodBase.GetCurrentMethod()?.DeclaringType?.Name ?? string.Empty);
            Console.WriteLine("Method: {0}",
                MethodBase.GetCurrentMethod()?.Name ?? string.Empty);
        }
    }
}

Notice line 4 which sets up the namespace to...
That's because with your four project, you added the extra code to the existing template, so you effectively have:
Program.cs in four:
using System;
using System.Reflection;

namespace four
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine("Namespace: {0}",
                MethodBase.GetCurrentMethod()?.DeclaringType?.Namespace ?? string.Empty);
            Console.WriteLine("Class: {0}",
                MethodBase.GetCurrentMethod()?.DeclaringType?.Name ?? string.Empty);
            Console.WriteLine("Method: {0}",
                MethodBase.GetCurrentMethod()?.Name ?? string.Empty);
        }
    }
}

Notice line 4 which sets up the namespace to be "four".

In your seven project, you are using top level statements. By default top level statements are put into a class named Program setup without a namespace. To get the same effect in your four project delete lines 4-5 and 19.
 
Solution
By default top level statements are put into a class named Program setup without a namespace.
Actually in a namespace without a name, the global namespace.
Top-level statements are implicitly in the global namespace.

So if you for example add class MyClass after the top level statements you could qualify it global::MyClass, but compiler will complain that is unnecessary (and also that types should be "in a namespace"...)
 

Latest posts

Back
Top Bottom