Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

285
Visualizações
When does Type.Namespace return null if instance represents a generic parameter

Documentation of Type.Namespace property states:

If the current Type represents a constructed generic type, this property returns the namespace that contains the generic type definition. Similarly, if the current Type represents a generic parameter T, this property returns the namespace that contains the generic type definition that defines T.

If the current Type object represents a generic parameter, this property returns null.

When does Type.Namespace return null if instance represents a generic parameter like in the last cited paragraph? Or is the documentation wrong and the the statements in bold are actually contradicting each other?

To investigate I wrote some simple code, but I haven't found any answer. All the Console.WriteLine statements print something.

using System;

namespace TypeDemo
{
    internal static class Program
    {
        public static void Main()
        {
            var g = new G<int>();
            g.M(4);

            Console.WriteLine(typeof(G<>).Namespace);
        }
    }

    public class G<T>
    {
        static G()
        {
            Console.WriteLine(typeof(T).Namespace);
        }

        public G()
        {
            Console.WriteLine(typeof(T).Namespace);
            Console.WriteLine(GetType().Namespace);
        }

        public void M<S>(S s)
        {
            Console.WriteLine(typeof(S).Namespace);
            Console.WriteLine(s.GetType().Namespace);
        }
    }
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Note that all the types that you are printing are not generic parameters. Notice how when the methods are run, the type is already constructed. For example, when you do:

var g = new G<int>();

// which calls...
public G()
{
    // T has become "int" here!
    Console.WriteLine(typeof(T).Namespace);
    Console.WriteLine(GetType().Namespace);
}

To get a Type that represents generic parameters, you can:

[call] the GetGenericArguments method of a Type object that represents a generic type definition, or the GetGenericArguments method of a MethodInfo object that represents a generic method definition.

Source

For example:

Console.WriteLine(typeof(Foo.C<>).GetGenericArguments()[0].Namespace);

namespace Foo {
    class C<T> {
        public void Foo() {
            
        }
    }
}

and

Console.WriteLine(typeof(Foo.C).GetMethod("Foo").GetGenericArguments()[0].Namespace);

namespace Foo {
    class C {
        public void Foo<T>() {
            
        }
    }
}

Both of them prints Foo, and I have not been able to find a case where a generic type parameter is in a namespace, but its Namespace returns null. This means the actual behaviour matches this sentence:

Similarly, if the current Type represents a generic parameter T, this property returns the namespace that contains the generic type definition that defines T.

over 4 years ago · Santiago Trujillo Relatório

0

Docs are contradicting, but correct in principle. Type.Namespace returns null when the namespace is not available from generic definition. One way to get this is MakeGenericMethodParameter:

Type t=Type.MakeGenericMethodParameter(0);
Console.WriteLine("IsGenericParameter: "+t.IsGenericParameter);
Console.WriteLine("Namespace: "+(t.Namespace==null?"(null)":t.Namespace));

Another is to have a custom Type implementation (it's an abstract class everyone could derive from and override properties to do whatever they want).

Edit: Type.Namespace docs were fixed in PR https://github.com/dotnet/dotnet-api-docs/pull/7466/files

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda