views:

304

answers:

3

Hi there is there a possibility 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.

With kind regards

Sebastian

+7  A: 

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.

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.)

Here's the LINQ query though:

public static IEnumerable<Type> GetTypesFromNamespace(Assembly assembly, 
                                               String desiredNamepace)
{
    return assembly.GetTypes()
                   .Where(type => type.Namespace == desiredNamespace);
}
Jon Skeet
A: 

thanks I think this should solve my problem, i will try it =)

Xelluloid
A: 

How to convert Namespace to Assembly?

This doesn't make sense... there is no relation between namespace and assembly. Classes in the same namespace can be in different assemblies, and an assembly can contain classes in different namespaces.
Thomas Levesque
I'm sorry. How to get all classes from namespace?