Listing all the classes in a DLL

大憨熊 提交于 2019-12-25 07:35:05

问题


I would like to list all of the classes that are in a DLL without having to load the dependencies. I don't want to execute any functionality, I just want to find out (problematically) what classes are inside of a given DLL. Is that possible? I've tried using the assembly.GetTypes() call, but it fails because of dependencies for executing the DLL. Is there any other way to list all the public classes?


回答1:


I suggest you use the mono Cecil library. This is the basic example:

//Creates an AssemblyDefinition from the "MyLibrary.dll" assembly
AssemblyDefinition myLibrary = AssemblyFactory.GetAssembly ("MyLibrary.dll");

//Gets all types which are declared in the Main Module of "MyLibrary.dll"
foreach (TypeDefinition type in myLibrary.MainModule.Types) {
    //Writes the full name of a type
    Console.WriteLine (type.FullName);
}

This will not load all the dependencies.




回答2:


You can use Assembly.ReflectionOnlyLoad method to load assembly without executing it.

How to: Load Assemblies into the Reflection-Only Context

Also you need to attach AppDomain.ReflectionOnlyAssemblyResolve as In the reflection-only context, dependencies are not resolved automatically.




回答3:


Okay, I found it. This combination works to get a list of all classes without having to deal with the dependencies:

Assembly assembly = Assembly.LoadFrom(filename); Type[] types = assembly.GetTypes();



来源:https://stackoverflow.com/questions/14093266/listing-all-the-classes-in-a-dll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!