Advertisement
Guest User

Untitled

a guest
Aug 6th, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.74 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4.  
  5. namespace GenericPluginMasterInterfaceNamespace
  6. {
  7.     public class GenericPluginServices
  8.     {
  9.         private Types.AvailablePlugins colAvailablePlugins = new Types.AvailablePlugins();
  10.  
  11.         /// <summary>
  12.         /// A Collection of all Plugins Found and Loaded by the FindPlugins() Method
  13.         /// </summary>
  14.         public Types.AvailablePlugins AvailablePlugins
  15.         {
  16.             get { return colAvailablePlugins; }
  17.             set { colAvailablePlugins = value; }
  18.         }
  19.  
  20.         /// <summary>
  21.         /// Searches the Application's Startup Directory for Plugins
  22.         /// </summary>
  23.         public void FindPlugins(Boolean clearFirst, LoggingUtilities.Source source)
  24.         {
  25.             FindPlugins(AppDomain.CurrentDomain.BaseDirectory, clearFirst, source);
  26.         }
  27.         /// <summary>
  28.         /// Searches the passed Path for Plugins
  29.         /// </summary>
  30.         /// <param name="Path">Directory to search for Plugins in</param>
  31.         public void FindPlugins(string Path, Boolean clearFirst, LoggingUtilities.Source source)
  32.         {
  33.             //First empty the collection, we're reloading them all
  34.             if (clearFirst)
  35.                 colAvailablePlugins.Clear();
  36.  
  37.             //Go through all the files in the plugin directory
  38.             foreach (String fileOn in Directory.GetFiles(Path))
  39.             {
  40.                 FileInfo file = new FileInfo(fileOn);
  41.  
  42.                 //Preliminary check, must be .dll
  43.                 if (file.Extension.Equals(".dll"))
  44.                 {
  45.                     //Add the 'plugin'
  46.                     this.AddPlugin(fileOn, source);
  47.                 }
  48.             }
  49.         }
  50.  
  51.         /// <summary>
  52.         /// Unloads and Closes all AvailablePlugins
  53.         /// </summary>
  54.         public void ClosePlugins()
  55.         {
  56.             foreach (Types.AvailablePlugin pluginOn in colAvailablePlugins)
  57.             {
  58.                 //Close all plugin instances
  59.                 //We call the plugins Dispose sub first incase it has to do
  60.                 //Its own cleanup stuff
  61.                 //pluginOn.Instance.Dispose();
  62.  
  63.                 //After we give the plugin a chance to tidy up, get rid of it
  64.                 pluginOn.Instance = null;
  65.             }
  66.  
  67.             //Finally, clear our collection of available plugins
  68.             colAvailablePlugins.Clear();
  69.         }
  70.  
  71.         private void AddPlugin(string FileName, LoggingUtilities.Source source)
  72.         {
  73.             //Create a new assembly from the plugin file we're adding..
  74.             Assembly pluginAssembly = Assembly.LoadFrom(FileName);
  75.  
  76.             try
  77.             {
  78.  
  79.                 //Next we'll loop through all the Types found in the assembly
  80.                 foreach (Type pluginType in pluginAssembly.GetTypes())
  81.                 {
  82.                     if (pluginType.IsPublic) //Only look at public types
  83.                     {
  84.                         if (!pluginType.IsAbstract)  //Only look at non-abstract types
  85.                         {
  86.                             //Gets a type object of the interface we need the plugins to match
  87.                             Type typeInterface = pluginType.GetInterface("GenericPluginMasterInterfaceNamespace.IGenericPluginMasterInterface", true);
  88.  
  89.                             //Make sure the interface we want to use actually exists
  90.                             if (typeInterface != null)
  91.                             {
  92.                                 //Create a new available plugin since the type implements the IPlugin interface
  93.                                 Types.AvailablePlugin newPlugin = new Types.AvailablePlugin();
  94.  
  95.                                 //Set the filename where we found it
  96.                                 newPlugin.AssemblyPath = FileName;
  97.  
  98.                                 //Create a new instance and store the instance in the collection for later use
  99.                                 //We could change this later on to not load an instance.. we have 2 options
  100.                                 //1- Make one instance, and use it whenever we need it.. it's always there
  101.                                 //2- Don't make an instance, and instead make an instance whenever we use it, then close it
  102.                                 //For now we'll just make an instance of all the plugins
  103.                                 newPlugin.Instance = (IGenericPluginMasterInterface)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  104.  
  105.                                 //Add the new plugin to our collection here
  106.                                 this.colAvailablePlugins.Add(newPlugin);
  107.  
  108.                                 //cleanup a bit
  109.                                 newPlugin = null;
  110.                             }
  111.  
  112.                             typeInterface = null; //Mr. Clean          
  113.                         }
  114.                     }
  115.                 }
  116.             }
  117.             catch (ReflectionTypeLoadException ex1)
  118.             {
  119.                 for (int a = 0; a < ex1.LoaderExceptions.Length; a++)
  120.                 {
  121.                     LoggingUtilities.LogFunctions.Log("", source, LoggingUtilities.MessageType.Error, ex1.LoaderExceptions[a]);
  122.                 }
  123.  
  124.                 Console.WriteLine(ex1.Message);
  125.             }
  126.             catch (Exception ex2)
  127.             {
  128.                 LoggingUtilities.LogFunctions.Log("", source, LoggingUtilities.MessageType.Error, ex2);
  129.                 Console.WriteLine(ex2.Message);
  130.             }
  131.  
  132.             pluginAssembly = null; //more cleanup
  133.         }
  134.     }
  135.  
  136.     namespace Types
  137.     {
  138.         /// <summary>
  139.         /// Collection for AvailablePlugin Type
  140.         /// </summary>
  141.         public class AvailablePlugins : System.Collections.CollectionBase
  142.         {
  143.             //A Simple Home-brew class to hold some info about our Available Plugins
  144.  
  145.             /// <summary>
  146.             /// Add a Plugin to the collection of Available plugins
  147.             /// </summary>
  148.             /// <param name="pluginToAdd">The Plugin to Add</param>
  149.             public void Add(Types.AvailablePlugin pluginToAdd)
  150.             {
  151.                 this.List.Add(pluginToAdd);
  152.             }
  153.  
  154.             /// <summary>
  155.             /// Remove a Plugin to the collection of Available plugins
  156.             /// </summary>
  157.             /// <param name="pluginToRemove">The Plugin to Remove</param>
  158.             public void Remove(Types.AvailablePlugin pluginToRemove)
  159.             {
  160.                 this.List.Remove(pluginToRemove);
  161.             }
  162.  
  163.             /// <summary>
  164.             /// Finds a plugin in the available Plugins
  165.             /// </summary>
  166.             /// <param name="pluginNameOrPath">The name or File path of the plugin to find</param>
  167.             /// <returns>Available Plugin, or null if the plugin is not found</returns>
  168.             public Types.AvailablePlugin Find(string pluginNameOrPath)
  169.             {
  170.                 Types.AvailablePlugin toReturn = null;
  171.  
  172.                 //Loop through all the plugins
  173.                 foreach (Types.AvailablePlugin pluginOn in this.List)
  174.                 {
  175.                     //Find the one with the matching name or filename
  176.                     if (pluginOn.AssemblyPath.Equals(pluginNameOrPath) ||
  177.                         String.Compare(pluginOn.Instance.returnName(), pluginNameOrPath) == 0)
  178.                     {
  179.                         toReturn = pluginOn;
  180.                         break;
  181.                     }
  182.                 }
  183.                 return toReturn;
  184.             }
  185.  
  186.             public Types.AvailablePlugin Get(int index)
  187.             {
  188.                 if (index > this.List.Count)
  189.                 {
  190.                     return null;
  191.                 }
  192.                 else
  193.                 {
  194.                     return (Types.AvailablePlugin)this.List[index];
  195.                 }
  196.             }
  197.         }
  198.  
  199.         /// <summary>
  200.         /// Data Class for Available Plugin.  Holds and instance of the loaded Plugin, as well as the Plugin's Assembly Path
  201.         /// </summary>
  202.         public class AvailablePlugin
  203.         {
  204.             //This is the actual AvailablePlugin object..
  205.             //Holds an instance of the plugin to access
  206.             //ALso holds assembly path... not really necessary
  207.             private IGenericPluginMasterInterface myInstance = null;
  208.             private string myAssemblyPath = "";
  209.  
  210.             public IGenericPluginMasterInterface Instance
  211.             {
  212.                 get { return myInstance; }
  213.                 set { myInstance = value; }
  214.             }
  215.             public string AssemblyPath
  216.             {
  217.                 get { return myAssemblyPath; }
  218.                 set { myAssemblyPath = value; }
  219.             }
  220.         }
  221.     }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement