jeudi 22 septembre 2022

C# What type in an assembly does not belong to a Namespace?

Console.WriteLine("Please choose a method to obtain a table:\n");

        Type[] current = Assembly.GetExecutingAssembly().GetTypes();
                    
        for(int i = 0, j = 1; i < current.Length; i++)
        {
            if (current[i].IsClass && current[i].Namespace.Contains("parser", StringComparison.InvariantCultureIgnoreCase) &&
                current[i].Name.Contains("parser", StringComparison.InvariantCultureIgnoreCase))
            {
                Console.WriteLine("{0}.{1}", j, current[i].Name);
                j++;
            }
        }

In this code the condition

current[i].Namespace

is highlighted as

Dereference of a possibly null reference.

I've checked Type metadata and found that the property returns null if the current instance has no namespace.

My question is what kind of type in an assembly could not belong to a Namespace?





Aucun commentaire:

Enregistrer un commentaire