Guest User

PluginService

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