Search code examples
c#reflectionsystem.reflection

Finding the public classes in a directory of files


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?


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.