Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I locate a specific type in an Assembly *efficiently*?

I'm looking for a more efficient way to find a type in an Assembly that derives from a known specific type. Basically, I have a plugin architecture in my application, and for the longest time we've been doing this:

For Each t As Type In assem.GetTypes()
    If t.BaseType Is pluginType Then
        'Do Stuff here'
    End If
Next

Some of the plugins have a large number of types and we're starting to see this take a few seconds. Is there any way I can just ask for all types that have a BaseType of "pluginType"?

EDIT: I over-simplified my code sample. I was using .GetExportedTypes() in my actual code. However, I alot of classes were marked as Public, so it wasn't helping too much. I combed through the projects and marked everything "Friend" except for the actual plugin class, and it still takes nearly the same amount of time to examine the assemblies. I cut maybe 100 ms off of 1.3 seconds (which is less than 10%). Is this just the minimum time I have to deal with? I'd also tried the Assembly Attribute suggestion and it still didn't yield much difference (maybe 100ms again). Is the rest of the time the overhead I have to pay to load assemblies dynamically?

like image 742
Bob King Avatar asked Feb 11 '09 14:02

Bob King


2 Answers

First off, you may try using GetExportedTypes() to narrow down the list of potential types. Other than that, there's no way you can speed up the iteration process. You can, however, include a plugin mainfest which would specify the exact type (types) of plugins within a particular assembly:

<manifest>
    <plugin type="FooBar.Plugins.BazPlugin, FooBar" />
</manifest>

so that you'd be able to do Type.GetType(string)

IPlugin plugin = (IPlugin)Type.GetType("manifest/plugin/@type" /* for the sake of this discussion */);
like image 147
Anton Gogolev Avatar answered Oct 09 '22 00:10

Anton Gogolev


Assembly.GetExportedTypes() only returns public classes. Could this help?

like image 42
BC. Avatar answered Oct 08 '22 22:10

BC.