取出一个特定的命名空间中的所有类 [英] Taking out all classes of a specific namespace
问题描述
有没有一种方式来获得从一个特定的命名空间的对象呢?或许与 System.Reflections
?我想从命名空间中的类型 ITestType
中的所有对象 Test.TestTypes
作为对象,让我有一个列表 TestType1,TestType2,TestType3实例
等。有人能帮助我吗?我不知道去哪里寻找这一点。
Is there a way to get an object from a specific namespace? Perhaps with the System.Reflections
? I want to get all objects from type ITestType
in the namespace Test.TestTypes
as Objects so that I have a list of instances of TestType1, TestType2, TestType3
and so on. Can Someone help me? I don't know where to search for that.
推荐答案
您可以找到所有的类型的内装配,并找到所有那些符合给定命名空间哪些类型的(这是LINQ真的很容易) - 但如果你没有一个特定的组件,通过看,你需要检查的所有的中。可能那些
You can find all the types within an assembly, and find all of those types which match the given namespace (this is really easy with LINQ) - but if you don't have a specific assembly to look through, you need to examine all of the possible ones.
不过,如果你正在寻找发现所有的活动的对象的方法的,这是一个不同的问题 - 你不能做到这一点,而不使用分析器API,据我所知。 (即使如此,它可能很难 - 我不知道。)
However, if you're looking for a way of finding all the live objects, that's a different matter - and you can't do it without using the profiler API, as far as I'm aware. (Even then it may be hard - I don't know.)
这里的LINQ查询,但:
Here's the LINQ query though:
public static IEnumerable<Type> GetTypesFromNamespace(Assembly assembly,
String desiredNamespace)
{
return assembly.GetTypes()
.Where(type => type.Namespace == desiredNamespace);
}
这篇关于取出一个特定的命名空间中的所有类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!