Question

This may be a long shot, but the flexibility of .NET never ceases to amaze me, so here goes.

I'm developing an MVC application that needs to search through a set of assemblies for class types that are derived from a common base class.

i.e. I have several assemblies that have activity types that are all derived from ActivityBase. These assemblies will be placed in a specific directory. They're kinda like plugins. Since they will need to be loaded dynamically at runtime, they will also need to be accompanied by some dependencies (unless I can figure out a convenient way to separate them, feel free to chime in on this as well.)

I have code that will iterate through all of the .DLL files in this directory, load the assembly, and iterate through all of the types in the assembly to find ones that are derived from ActivityBase.

This works fine. However, I would like to avoid loading and searching through the assemblies that do not have activities, because some of the dependencies have thousands of types and it becomes a performance problem.

So I guess my question is, other than a file naming convention, is there any way to "decorate" or mark an assembly with some type of data that would indicate that it is an activity assembly, that can be easily automated at build time and easily read at runtime?

Any other suggestions for handling this problem are also welcome.

Was it helpful?

Solution

You can implement your own assembly attribute:

[AttributeUsage(AttributeTargets.Assembly)]
public class ActivityAssemblyAttribute : Attribute
{
}

Then decorate necessary assembly with this attribute:

[assembly: ActivityAssemblyAttribute()]

And, when needed, just check whether the assembly under question is decorated with this attribute:

ActivityAssemblyAttribute attribute = null;
object[] attributes = assembly.GetCustomAttributes(typeof(ActivityAssemblyAttribute), false);
if (attributes.Length > 0)
{
   attribute = attributes[0] as ActivityAssemblyAttribute;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top