Skip to content

Instantly share code, notes, and snippets.

@MessiDaGod
Created July 27, 2022 17:24
Show Gist options
  • Save MessiDaGod/c64df3aac3b26760a8c5a7ee5f1c1ddb to your computer and use it in GitHub Desktop.
Save MessiDaGod/c64df3aac3b26760a8c5a7ee5f1c1ddb to your computer and use it in GitHub Desktop.
Get all types of a certain name from assembly C#
public List<Type>? GetAssemblyTypes(string typeName, string assemblyName = "Shakely")
{
List<Assembly> allAssemblies = new List<Assembly>();
List<Type> allTypes = new List<Type>();
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
for (int i = 0; i < Directory.GetFiles(path, $"{assemblyName}*.dll").ToArray().Length; i++)
{
var dll = Assembly.LoadFile(Directory.GetFiles(path, $"{assemblyName}*.dll").ToArray()[i]);
allAssemblies.Add(dll);
for (int j = 0; j < dll.GetTypes().ToArray().Length; j++)
{
var type = dll.GetTypes().ToArray()[j];
if (type.Name == typeName)
{
allTypes.Add(type);
}
}
}
return allTypes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment