I have a function that receive a type and returns true or false. I need to find out what are all types in a certain namespace that that function will return true for them. Thanks.
Asked
Active
Viewed 2,695 times
0
-
Not strictly related to a foreach loop, but similar question: http://stackoverflow.com/questions/79693/getting-all-types-in-a-namespace-via-reflection – Utaal Jul 16 '09 at 10:01
-
1Do you mean by "all types" all the types *anywhere*, or do you merely mean all types *in a given assembly*? Because if you mean the former, how do you know I'm not writing an assembly right now on my machine that has a type in the namespace you're interested in? – Eric Lippert Jul 16 '09 at 13:41
-
I mean all types that are availble at runtime. – Clangon Jul 16 '09 at 16:53
-
Define "available". Suppose I put my assembly on a public internet site but do not tell you about it. Is it "available"? You are free to download it and use it at runtime, but if you don't know it is there, it's going to be hard to search it for types in the namespace you're interested in. – Eric Lippert Jul 16 '09 at 18:19
-
2The reason I ask is because all the solutions so far only give the types in a namespace _in a given assembly_, which is not the question you asked. Remember, namespaces are _larger_ than assemblies; you can have one namespace that is in a thousand different assemblies. – Eric Lippert Jul 16 '09 at 18:21
-
I mean all assemblies that were loaded till the moment my code is running. – Clangon Jul 17 '09 at 09:12
3 Answers
10
Here's a function that gets all the classes in a namespace:
using System.Reflection;
using System.Collections.Generic;
/// <summary>
/// Method to populate a list with all the class
/// in the namespace provided by the user
/// </summary>
/// <param name="nameSpace">The namespace the user wants searched</param>
/// <returns></returns>
static List<string> GetAllClasses(string nameSpace)
{
//create an Assembly and use its GetExecutingAssembly Method
//http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.getexecutingassembly.aspx
Assembly asm = Assembly.GetExecutingAssembly();
//create a list for the namespaces
List<string> namespaceList = new List<string>();
//create a list that will hold all the classes
//the suplied namespace is executing
List<string> returnList = new List<string>();
//loop through all the "Types" in the Assembly using
//the GetType method:
//http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.gettypes.aspx
foreach (Type type in asm.GetTypes())
{
if (type.Namespace == nameSpace)
namespaceList.Add(type.Name);
}
foreach (String className in namespaceList)
returnList.Add(className);
return returnList;
}
[Update]
Here's a more compact way to do it, but this requires .Net 3.5 (from here):
public static IEnumerable<Type> GetTypesFromNamespace(Assembly assembly,
String desiredNamepace)
{
return assembly.GetTypes()
.Where(type => type.Namespace == desiredNamespace);
}

Community
- 1
- 1

Andreas Grech
- 105,982
- 98
- 297
- 360
1
System.Reflection.Assembly.GetTypes()
: Gets all types defined in this assembly.*
Should do the job =)

Andreas Grech
- 105,982
- 98
- 297
- 360

Clement Herreman
- 10,274
- 4
- 35
- 57
1
Add all the assemblies you want to search and implement the MyFunction method:
class Program
{
static void Main(string[] args)
{
List<Assembly> assembliesToSearch = new List<Assembly>();
// Add the assemblies that you want to search here:
assembliesToSearch.Add(Assembly.Load("mscorlib")); // sample assembly, you might need Assembly.GetExecutingAssembly
foreach (Assembly assembly in assembliesToSearch)
{
var typesForThatTheFunctionReturnedTrue = assembly.GetTypes().Where(type => MyFunction(type) == true);
foreach (Type type in typesForThatTheFunctionReturnedTrue)
{
Console.WriteLine(type.Name);
}
}
}
static bool MyFunction(Type t)
{
// Put your logic here
return true;
}
}

weiqure
- 3,247
- 2
- 27
- 31