Question

I know using reflection that I can find the list of classes within a single assembly (such an example is How to get all classes in current project using reflection?).

Is there a way to do this with just public classes within a given directory?

Was it helpful?

Solution

I assume you mean a directory that contains one or more assemblies, if so you can do it like this:

var types = new List<Type>();

var paths = Directory.GetFiles("directoryPath", "*.dll", SearchOption.TopDirectoryOnly);

foreach(var path in paths)
{
    types.AddRange(Assembly.LoadFrom(path).GetTypes());
}

GetTypes method uses BindingFlags.Public and BindingFlags.Instance by default.So you don't need to specify BindingFlags parameter additionally.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top