如何从泛型类型中获取命名空间?

bin*_*eol 4 c# generics namespaces

我有一个我指定为某种类型的方法.

例如

public MyLabels GetLabels<T>()
{
    // I'd like to get the namespace of the type that T represents here
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

gdo*_*ica 7

使用以下Namespace属性Type:

typeof(T).Namespace
Run Code Online (Sandbox Code Playgroud)

MSDN


Eba*_*ood 6

typeof(T).FullName // namespace and class name 
typeof(T).Namespace // namespace, no class name
Run Code Online (Sandbox Code Playgroud)


O. *_*per 2

您可以使用typeof(T).FullName

该字符串包含类名和命名空间。