How to get the full name of a namespace as a string in C#?
Asked Answered
L

2

5

I'm trying to get the full name of a namespace and convert it to a string.

Specifically, I have the namespace DevExpress.Xpf and I would like nameof2(DevExpress.Xpf) or some equivalent to return "DevExpress.Xpf", rather than the "Xpf" that nameof returns.

Currently, I am using $"{nameof(DevExpress)}.{nameof(DevExpress.Xpf)}" to achieve this end.

Is it possible to use reflection or some other feature of C# to get the full address of a namespace as a string?

Langdon answered 25/3, 2022 at 13:27 Comment(1)
Does this answer your question? How can I retrieve the namespace to a string C#Glamorous
N
10

The Namespace property of the Type object returns the fully qualified name of the namespace the type's declared in. So considering you have a class named A in the namespace DevExpress.Xpf, calling typeof(A).Namespace will return "DevExpress.Xpf".

Nahshunn answered 25/3, 2022 at 14:35 Comment(1)
Unfortunately this doesn't work on namespaces themselves, only types.Protectorate
J
-3

I'm not on a on pc that can run C# but something like this should work.

string name = nameof(DevExpress.Xpf);

(i'm assumming DevExpress.Xpf is the correct namespace name because you wrote that in the question)

Jo answered 25/3, 2022 at 13:34 Comment(1)
That would just return the string "Xpf"Langdon

© 2022 - 2024 — McMap. All rights reserved.