如何获得命名空间内的所有类? [英] How to get all classes within namespace?

查看:168
本文介绍了如何获得命名空间内的所有类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

      
  • <一个href="http://stackoverflow.com/questions/343869/taking-out-all-classes-of-a-specific-namespace">Taking一个特定的命名空间中的所有类
  •   
  • <一个href="http://stackoverflow.com/questions/79693/getting-all-types-in-a-namespace-via-reflection">Getting所有类型的命名空间通过反射
  •   

Possible Duplicates

  • Taking out all classes of a specific namespace
  • Getting all types in a namespace via reflection

对不起。如何获得命名空间内的所有类?

Excuse me. How to get all classes within namespace?

推荐答案

您需要做的是倒退;列出的组件,然后检查每个类型的命名空间的所有类型:

You will need to do it "backwards"; list all the types in an assembly and then checking the namespace of each type:

using System.Reflection;
private Type[] GetTypesInNamespace(Assembly assembly, string nameSpace)
{
    return assembly.GetTypes().Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal)).ToArray();
}

使用示例:

Type[] typelist = GetTypesInNamespace(Assembly.GetExecutingAssembly(), "MyNamespace");
for (int i = 0; i < typelist.Length; i++)
{
    Console.WriteLine(typelist[i].Name);
}

这篇关于如何获得命名空间内的所有类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆