1

I want to get a namespace string from a strongly typed namespace reference in a similar way as C# 6 nameof() works for types and type members.

namespace This.Is.My.Namespace
{
    class Program
    {
        static void Main()
        {
            string namespaceString = nameof(This.Is.My.Namespace);
            Console.Write(namespaceString);
        }
    }
}

But the result i get is only the last portion of the namespace (e.g. "Namespace"). I'm targeting .NET 4.6

Is this possible?

REMARK: I want to be able to reference any available namespace, not only the containing one.

4
  • The first sentence from the page you linked to says "Used to obtain the simple (unqualified) string name of a variable, type, or member". But this sounds like an X-Y problem: why do you need to do this to get the namespace name when you can get it from a type in that namespace? Commented Jun 8, 2016 at 12:40
  • @stuartd for example i want to filter certain types coming from an assembly reflection Commented Jun 8, 2016 at 12:51
  • 1
    Why don't you then show an example of what you are actually trying to do? Commented Jun 8, 2016 at 12:55
  • @stuartd Because this is more general question. I'm looking for a clean and flexible way to express the namespace not as a hardcoded literal which will be hard to refactor later. Commented Jun 8, 2016 at 13:02

3 Answers 3

2

This should work...

var myType = this.GetType();
var namespaceString = myType.Namespace;
Sign up to request clarification or add additional context in comments.

6 Comments

typeof(this) won't compile. Maybe this.GetType() or typeof(Program).
And you don´t know any Type within that namespace? A Namespace without any types in it will not be accessible at runtime to my knowledge, since it will not contain anything.
@Marcus it is odd to choose a specific type if you have a number of types contained in the same namespace. I really like the clearness of the nameof() syntax.
But what do you think a namespace really is, because I think you misunderstand the concept. A namespace is just a container. It really is nothing and there is no reference to it (does not occupy memory) at runtime. Without a Type - the namespace does not exist in a runtime context. Maybe we could help better if you broaden your question and explain what you are trying to achieve?
I guess my point is that since there is no reference to a namespace in itself (at runtime) there is no way to get the namespace name as a string since there is nothing you can retrieve it from. You will need a type or an instance of a type within the namespace.
|
2

nameof is replaced at compile time with string, where string is an exact text of that member (as if you would put it yourself inside "").

To achieve what you want you can construct something monstrous-like:

namespace This.Is.My.Namespace
...
var namespaceString = $"{nameof(This)}.{nameof(This.Is)}.{nameof(This.Is.My)}.{nameof(This.Is.My.Namespace)}";

This will ensure what every member exists at compile-time and allow easy refactoring of either (well, unless you decide to add/delete some names).

P.S.: but obviously other answers are more efficient at generating same string.

1 Comment

Clever, but really hard to read and to type. It's a step closer to my goal though.
2

For string use below code

    Type myType = typeof(MyClass);
    var n = myType.Namespace;

Type myType = typeof(MyClass);
Console.WriteLine("Namespace: {0}.", myType.Namespace);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.