0

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?

Community
  • 1
  • 1
Nodoid
  • 1,449
  • 3
  • 24
  • 42
  • 1
    What is "*a directory*"? Are you referring to an actual folder on your system? – Jeroen Vannevel Apr 30 '14 at 15:12
  • for example, my project has a directory called "fun_classes" which contains a pile of SQL classes for the creation of tables. I'd be looking to examine that directory and extract the class names - can equally apply to any directory though. – Nodoid Apr 30 '14 at 15:14
  • I deleted my answer since the link you included does actually include an answer that looks for public classes – Sayse Apr 30 '14 at 15:15

1 Answers1

1

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.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184