Created
July 27, 2022 17:24
-
-
Save MessiDaGod/c64df3aac3b26760a8c5a7ee5f1c1ddb to your computer and use it in GitHub Desktop.
Get all types of a certain name from assembly C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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