简体   繁体   中英

How can I get the namespace from a generic type?

I have a method that I specify to be of a certain type.

Eg

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

How can I do this?

Use the Namespace property of Type :

typeof(T).Namespace

MSDN

typeof(T).FullName // namespace and class name 
typeof(T).Namespace // namespace, no class name

You can use typeof(T).FullName .

That string contains both the class name and the namespace.

怎么样

typeof(T).Namespace;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM