This question already has answers here:
Closed 7 years ago.
I need to programmatically get a List
of all the classes in a given namespace. How can I achieve this (reflection?) in C#?
var theList = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.Namespace == "your.name.space")
.ToList();
Without LINQ:
Try:
Type[] types = Assembly.GetExecutingAssembly().GetTypes();
List<Type> myTypes = new List<Type>();
foreach (Type t in types)
{
if (t.Namespace=="My.Fancy.Namespace")
myTypes.Add(t);
}
Take a look at this How to get all classes within namespace? the answer provided returns an array of Type[] you can modify this easily to return List
I can only think of looping through types in an assebly to find ones iin the correct namespace
public List<Type> GetList()
{
List<Type> types = new List<Type>();
var assembly = Assembly.GetExecutingAssembly();
foreach (var type in assembly .GetTypes())
{
if (type.Namespace == "Namespace")
{
types.Add(type);
}
}
return types;
}