When you create a system that is related to reflection, you should define your own attributes for your own types, methods, and attributes so that they can be accessed more easily. Custom features indicate how you want these methods to be used at run time. Attributes can test properties on some target objects. Testing these properties minimizes the type errors that can result from reflection.
Suppose you need to create a mechanism to add a menu entry to a command handle on the Run-time software. This needs to be simple: put an assembly into the directory, and then the program can find some new menu items and new menu commands about it. This is one of the best things you can do with reflection: your main program needs to interact with some of the assemblies that have not yet been written. The new plugin also does not have to describe the functionality of a collection, as it is a good way to use the interface to complete the encoding.
Let's start writing code for a plugin that creates a framework. You need to use the Assembly.LoadFrom () function to load a program, and to find the type that might provide a menu handle. You then need to create an instance object of this type. You will then find a way to match the declaration of the menu command event handle on this instance object. After completing these tasks, you will also need to calculate where to add text and what text is in the menu.
Features make all of these tasks simple. By tagging different classes and event handlers with their own defined attributes, you can simply accomplish these tasks by discovering and installing these potential command handles. You can use features and reflexes to work together to minimize some of the dangerous things that are described in principle 43.
The first task is to write code that discovers and loads the plug-in assembly. Suppose the plug-in is in a subdirectory of the directory where the main execution program resides. The code to find and load this assembly is simple:
// Find all the assemblies in the Add-ins directory:
string AddInsDir = string.Format( " {0}/Addins",
Application.StartupPath );
string[] assemblies = Directory.GetFiles( AddInsDir, "*.dll" );
foreach ( string assemblyFile in assemblies )
{
Assembly asm = Assembly.LoadFrom( assemblyFile );
// Find and install command handlers from the assembly.
}
Next, you need to replace the last line of comments above with code that finds the classes that implement the command handle and installs the handles. After the full assembly is loaded, you can use reflection to find all the exposed types on the assembly, using attributes to identify which exposed types contain command handles and which are the methods of the command handle. The following is a class with attributes added, that is, the command handle type is marked:
// Define the Command Handler Custom Attribute:
[AttributeUsage( AttributeTargets.Class )]
public class CommandHandlerAttribute : Attribute
{
public CommandHandlerAttribute( )
{
}
}
This feature is all the code you need to tag for each command. Always mark an attribute class with the AttributeUsage attribute, which is to tell other programs and compilers where this feature can be used. The previous example shows that Commandhandlerattribute can only be used on a class, and it cannot be applied to elements of other languages.
You can call GetCustomAttributes to determine whether a class has a Commandhandlerattribute attribute. Only the type that has this attribute is the candidate type of the plug-in:
// Find all the assemblies in the Add-ins directory:
string AddInsDir = string.Format( "{0}/Addins", Application.StartupPath);
string[] assemblies = Directory.GetFiles( AddInsDir, "*.dll" );
foreach ( string assemblyFile in assemblies )
{
Assembly asm = Assembly.LoadFrom( assemblyFile );
// Find and install command handlers from the assembly.
foreach( System.Type t in asm.GetExportedTypes( ))
{
if (t.GetCustomAttributes (
typeof( CommandHandlerAttribute ), false ).Length > 0 )
{
// Found the command handler attribute on this type.
// This type implements a command handler.
// configure and add it.
}
// Else, not a command handler. Skip it.
}
}