The reflection mechanism provided by .NET can easily load plug-ins. This article provides a method that can flexibly and correctly load the required plug-ins.
In .NET, a complete type name has the format "typename, 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 complete type name with namespace.
You can also get the FullName using the type.
For example: string typeName = typeof(NameValueSectionHandler).FullName;
The assembly name is: "System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
The assembly is named System, and the system automatically adapts its extension (such as System.dll or System.exe);
Version, Culture, and PublicKeyToken are the specific version, cultural background, and signature of the assembly. There are no specific requirements, and these can be omitted.
We can dynamically load a required type based on the name of the type. like:
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 required type instance.
Usually plug-ins are classes that need 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 in the following way:
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 plugin
}
Summarizing the above code, we can make a general code for loading plug-ins:
/**//// <summary>
/// Dynamically load 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">Specify the parameters of the constructor (null or empty array means calling the default constructor)</param>
/// <returns>Returns the created type (null means 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-ins are generally placed in configuration files and read by the program:
Configuration file examples (see my related essays for the use of configuration files):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="Channels" type="Vmp.Configuration.ChannelsSectionHandler, Communication" />
</configSections>
<Channels>
<channel
ChannelType="Vmp.Communication.TcpChannel, Communication"
TraceFile="d:logchannel1.log"
Port="2020" MaxConnections="300" BufferSize="2048"
/>
</Channels>
</configuration>
Code example:
private ArrayList channelsList = new ArrayList();
private LoadChannels()
{
ArrayList channelsConfig = (ArrayList)ConfigurationSettings.GetConfig( "Channels" );
foreach(Hashtable config in channelsConfig)
{
string channelType = (string) config["ChannelType"];
IChannel channel = (IChannel) CommonUtils.LoadObject(channelType, typeof(IChannel).FullName, new object[]{config});
if(channel == null)
continue;
channelsList.Add(channel);
}
You can also traverse the specified plug-in directory and load all plug-ins that meet the requirements, for example:
public IPlugin[] LoadAllPlugIn(string pluginDir)
{
//Set the default plugin directory
if(pluginDir == null || pluginDir == "")
pluginDir = "./PlugIns";
// Get the plug-in interface name
string interfaceName = typeof(IPlugin).FullName;
// Array used to store plug-ins
ArrayList arr = new ArrayList();
// Traverse the plug-in directory (assuming the plug-in is a dll file)
foreach(string file in Directory.GetFiles(pluginDir, "*.dll"))
{
//Load the plug-in file
Assembly asm = Assembly.LoadFile(file);
// Traverse the exported plug-in classes
foreach(Type t in asm.GetExportedTypes())
{
//Load the plug-in. If the plug-in does not conform to the specified interface, return null
IPlugin plugin = LoadObject(t.FullName, interfaceName, null) as IPlugin;
if(plugin != null)
arr.Add(plugin);
}
}
// Return to plugin
return (IPlugin[])arr.ToArray(typeof(IPlugin));
}