BotNavSim  v0.4.3
Mobile Robot Simulation
PluginFactory< T > Class Template Reference

Plugin factory utility class instantates interfaces from a DLL assembly file Adapted from: http://stackoverflow.com/questions/5751844/how-to-reference-a-dll-on-runtime More...

Public Member Functions

CreatePlugin (string file)
 Find and instantiate a type from a specified assembly file (accepts only DLL files) More...
 
List< string > ListPlugins (string path)
 Lists assembly (DLL) file names that implement the type T. More...
 

Detailed Description

Plugin factory utility class instantates interfaces from a DLL assembly file Adapted from: http://stackoverflow.com/questions/5751844/how-to-reference-a-dll-on-runtime

Definition at line 13 of file PluginFactory.cs.

Member Function Documentation

T PluginFactory< T >.CreatePlugin ( string  file)

Find and instantiate a type from a specified assembly file (accepts only DLL files)

Returns
The plugin.
Parameters
fileFile.

Definition at line 20 of file PluginFactory.cs.

20  {
21  if (!file.EndsWith(".dll")) {
22  file += ".dll";
23  }
24  if (!File.Exists(file)) {
25  Debug.LogError("File not found (" + file + ")");
26  return default(T);
27  }
28  Type[] assemblyTypes = Assembly.LoadFrom(file).GetTypes();
29  foreach(Type assemblyType in assemblyTypes) {
30  Type interfaceType = assemblyType.GetInterface(typeof(T).FullName);
31  // if our interface is found, instantiate and return it!
32  if (interfaceType != null) {
33  return (T)Activator.CreateInstance(assemblyType);
34  }
35  }
36  Debug.LogError("Interface " + typeof(T).FullName + " not found in file: " + file);
37  return default(T);
38  }

Here is the caller graph for this function:

List<string> PluginFactory< T >.ListPlugins ( string  path)

Lists assembly (DLL) file names that implement the type T.

Returns
The plugins.
Parameters
pathPath.

Definition at line 45 of file PluginFactory.cs.

45  {
46  List<string> list = new List<string>();
47  // find .dll files
48  foreach (string file in Directory.GetFiles(path, "*.dll")) {
49  Assembly assembly;
50  try {
51  assembly = Assembly.LoadFrom(file);
52  }
53  catch(Exception e) {
54  Debug.LogError(file + ": " + e + " " + e.Message + ")");
55  continue;
56  }
57  Type[] types;
58  try {
59  types = assembly.GetTypes();
60  }
61  catch(Exception e) {
62  Debug.LogError(file + ": " + e + " " + e.Message + ")");
63  continue;
64  }
65  foreach (Type assemblyType in types) {
66  Type interfaceType = assemblyType.GetInterface(typeof(T).FullName);
67  if (interfaceType != null) {
68  list.Add(Path.GetFileNameWithoutExtension(file));
69  }
70  }
71  }
72 
73  return list;
74  }

Here is the caller graph for this function:


The documentation for this class was generated from the following file: