Get full nameof

I have something like this:
nameof(ClassA.ClassB.ClassC.ClassD.item
And as expected it returns “item”, however I need the full of that thing I’m referencing, “ClassA.ClassB.ClassC.ClassD.item”. I cannot use typeof().Name because sometimes I have to compare it against a namespace, and these two don’t work together.

Google tells me, c# - Get type name without full namespace - Stack Overflow

typeof(item).FullName

And my post tells me:

First post still unedited.

Could you clarify your question?

I’m not sure what you mean by comparing it against a namespace. FullName includes the namespace.

1 Like

Is this not the answer?

code:

        Debug.Log( "typeof(MonoBehaviour).ToString() -> " + typeof(MonoBehaviour).ToString());

The item is the namespace. I don’t have a class of which I need a namespace, the item I’m comparing against is a namespace, it’s not a class. typeof() doesn’t work on namespaces.

typeof(UnityEngine.UI).ToString()
Type name expected, but namespace name found

I don’t think namespaces exist “bare” like that. They need at least one class.

But this works as expected:

namespace UnityEngine
{
    public class MyClass : MonoBehaviour
    {
        void Start ()
        {
            Debug.Log( "typeof(MyClass).ToString() -> " + typeof(MyClass).ToString());
        }
    }
}

I just wedged a class into the UnityEngine namespace… now bust out your string-flicking skills.

1 Like

Well, I guess I’ll have to do it dirty. Thanks.

wut?

You could just pick a type in the namespace and do something like:

typeof(UnityEngine.UI.Image).Namespace

Unfortunately, this

was not very clear from this

Why doesn’t comparing directly to a string work? Are you trying to use symbols in case of later renaming? A code example would do a long way to help us understand what your concerns are. Based on the idea you just want to avoid using strings directly incase things are renamed later, this may work:

namespace Foo.Bar.Hello.World
{
  // ...
}

// ...

if (type.Namespace == $"{nameof(Foo)}.{nameof(Bar)}.{nameof(Hello)}.{nameof(World)}")
  // ...

Also, this sounds like the kind of question you ask after making a few wrong turns. I suggest asking yourself why you want to do this, recursively, 5 times and consider if you made the right design choice at each step.

2 Likes

Yes, this is my conclusion as well ^^. Namespaces are not really a thing. They are just type name prefixes to solve ambiguities across types, nothing more. They are essentially just part of the classname itself. In plain IL code there are no namespaces, just full typenames, everywhere.