The. NET-provided reflection (Reflection) mechanism makes it easy to load plug-ins. This article provides a way to flexibly load the required plug-ins correctly.
In. NET, a complete type name is formatted as "type name, assembly name."
For example: "System.Configuration.NameValueSectionHandler, System, version=1.0.3300.0, Culture=neutral, publickeytoken= b77a5c561934e089 ".
- The type name is: System.Configuration.NameValueSectionHandler, which is the full type name with the namespace.
You can also use this type of FullName to get it.
such as: string typeName = typeof (NameValueSectionHandler). FullName;
- The assembly name is: "System, version=1.0.3300.0, Culture=neutral, publickeytoken=b77a5c561934e089",
The assembly name is system, which is automatically fitted with an extension (such as System.dll or System.exe);
Version, Culture, PublicKeyToken for specific versions of the assembly, cultural background, signature, no specific requirements, these can be omitted.
We can dynamically load a desired type according to the name of the type. Such as:
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);
At this point, obj is the type instance that is required.
The usual plug-ins are classes that need to implement a certain interface. Therefore, before loading the plug-in, you need to determine whether the plug-in type is appropriate.
For example, a plug-in interface is IPlugin, so we can identify it in the following ways:
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 a required plugin
}
Summing up the above code, we can make a general code to load Plug-ins:
///<summary>
///dynamically mount and create a type that has the specified interface
///</summary>
///<param name= "ClassName" > type name </param>
///<param name= "InterfaceName" > Specified interface name </param>
///<param name= "param" > Specifies the parameters of the constructor (null or empty array to invoke the default constructor) </param>
///<returns> returns the type created (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.
Plug-in configuration, there are generally two ways:
1. Write the plug-in type name in the configuration file, such as App.config. The use of configuration files can refer to other articles in the MU Maple column.
2. Specify a directory for the plug-in directory, and then all the assemblies in the directory (such as *.dll) all loaded, and the reflection mechanism to get all the common types, and all try to load with Loadobject, loaded successfully is the appropriate plug-ins.