获取功能列表的一个DLL [英] Get a list of functions for a DLL
问题描述
我要建一个项目。它的用户可以添加自己的扩展(DLL文件)的应用程序。要管理的延伸,我需要的类,它的功能列表等是否有code这样做呢?
I'm building a project. It's an application that users can add their extensions (DLL files) to. To manage an extension, I need to get its list of classes, functions, etc. Is there code for doing it?
注:我使用C#
推荐答案
对于特定的组件,你可以使用<一个href="http://msdn.microsoft.com/en-us/library/system.reflection.assembly.gettypes.aspx"><$c$c>Assembly.GetTypes$c$c>得到的类型,则对于每个类型的呼叫<一href="http://msdn.microsoft.com/en-us/library/system.type.getmethods.aspx"><$c$c>Type.GetMethods()$c$c>, <一href="http://msdn.microsoft.com/en-us/library/system.type.getproperties.aspx"><$c$c>Type.GetProperties()$c$c>等,或只是<一href="http://msdn.microsoft.com/en-us/library/system.type.getmembers.aspx"><$c$c>Type.GetMembers()$c$c>.
For a particular assembly, you can use Assembly.GetTypes
to get the types, then for each type call Type.GetMethods()
, Type.GetProperties()
etc, or just Type.GetMembers()
.
不过,对于插件的功能,它通常有一个共同的接口,该插件必须执行一个好主意 - 可以减少反射,你需要使用量。使用<一个href="http://msdn.microsoft.com/en-us/library/system.type.isassignablefrom.aspx"><$c$c>Type.IsAssignableFrom()$c$c>检查一个类型是否是与特定的接口兼容。
However, for plugin functionality it's usually a good idea to have a common interface which the plugins have to implement - that reduces the amount of reflection you need to use. Use Type.IsAssignableFrom()
to check whether a type is compatible with a particular interface.
您可能也想看看托管扩展框架它可以使实施一个推广体系更加容易。
You might also want to look at the Managed Extensibility Framework which can make implementing an extension system easier.
这篇关于获取功能列表的一个DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!