The Reflection mechanism provided by. NET can easily load plug-ins. This article provides a method to flexibly and correctly load the required plug-ins.
In. NET, a complete type name format is as follows: "type name, Set Name ".
For example: "System. Configuration. NameValueSectionHandler, System, Version = 1.0.3300.0, Culture = neutral, PublicKeyToken = b77a5c561934e089 ".
- Type name: System. Configuration. NameValueSectionHandler. This is the complete type name with namespace.
You can also use FullName of this type.
For example, string typeName = typeof (NameValueSectionHandler). FullName;
- Assembly name: "System, Version = 1.0.3300.0, Culture = neutral, PublicKeyToken = b77a5c561934e089 ",
The program set name is system, and the system is automatically configured with an extension name (such as system.dllor system.exe );
Version, Culture, and PublicKeyToken are specific versions, cultural backgrounds, and signatures of the Assembly. They can be omitted without specific requirements.
We can dynamically load a required type based on the Type name. For example:
String typeName = "System. Configuration. NameValueSectionHandler, System ";
Type t = Type. GetType (typeName );
Object obj = Activator. CreateInstance (t );
Or
System. Configuration. NameValueSectionHandler obj = (System. Configuration. NameValueSectionHandler) Activator. CreateInstance (t );
In this case, obj is the required type instance.
A common plug-in is a class that needs to implement certain interfaces. Therefore, before loading a plug-in, you need to determine whether the plug-in type is appropriate.
For example, if the interface of a plug-in is IPlugin, we can identify it as follows:
String interfaceName = typeof (IPlugin). FullName;
String typeName = "Muf. MyPlugin, MyPlugin ";
Type t = Type. GetType (typeName );
If (t = null
|! T. IsClass
|! T. IsPublic
| T. GetInterface (interfaceName) = null)
{
Return null; // not the required plug-in
}
To sum up the above code, we can make general code for loading plug-ins:
/// <Summary>
/// Dynamically load and create a type with the specified Interface
/// </Summary>
/// <Param name = "className"> type name </param>
/// <Param name = "interfaceName"> specified interface name </param>
/// <Param name = "param"> specify the parameters of the constructor (null or an empty array indicates that the default constructor is called) </param>
/// <Returns> return the created type (null indicates that the type cannot be created or cannot be found) </returns>
Public static object LoadObject (string className, string interfaceName, object [] param)
{
Try
{
Type t = Type. GetType (className );
If (t = null
|! T. IsClass
|! T. IsPublic
| T. IsAbstract
| T. GetInterface (interfaceName) = null)
{
Return null;
}
Object o = Activator. CreateInstance (t, param );
If (o = null)
{
Return null;
}
Return o;
}
Catch (Exception ex)
{
Return null;
}
}
Later, we can use LoadObject to load any required plug-ins.
There are two methods to configure plug-ins:
1. Write the plug-in type name in the configuration file, such as app. config. For details about how to use the configuration file, refer to other articles in the Mu Feng column.
2. specify a directory as the plug-in directory, and then all the assembly (such *. dll) load all, use the reflection mechanism to obtain all the public types, and try to load all the files with LoadObject. The appropriate plug-in is loaded successfully.