Advertisement
Pearlfromsu

assembly in c#

Dec 28th, 2022
1,516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.51 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Windows.Forms;
  6. using CommonSnappableTypes;
  7.  
  8. namespace MyExtendableApp
  9. {
  10.     class Program
  11.     {
  12.         [STAThread]
  13.         static void Main(string[] args)
  14.         {
  15.             Console.WriteLine("***** Welcome to MyTypeViewer *****");
  16.             do
  17.             {
  18.                 Console.WriteLine("\nWould you like to load a snapin? [Y,N]");
  19.  
  20.                 // Get name of type.
  21.                 string answer = Console.ReadLine();
  22.  
  23.                 // Does user want to quit?
  24.                 if (!answer.Equals("Y", StringComparison.OrdinalIgnoreCase))
  25.                 {
  26.                     break;
  27.                 }
  28.  
  29.                 // Try to display type.
  30.                 try
  31.                 {
  32.                     LoadSnapin();
  33.                 }
  34.                 catch (Exception ex)
  35.                 {
  36.                     Console.WriteLine("Sorry, can't find snapin");
  37.                 }
  38.             } while (true);
  39.         }
  40.  
  41.         static void LoadSnapin()
  42.         {
  43.             // Allow user to select an assembly to load.
  44.             OpenFileDialog dlg = new OpenFileDialog
  45.             {
  46.                 //set the initial directory to the path of this project
  47.                 InitialDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
  48.                 Filter = "assemblies (*.dll)|*.dll|All files (*.*)|*.*",
  49.                 FilterIndex = 1
  50.             };
  51.  
  52.             if (dlg.ShowDialog() != DialogResult.OK)
  53.             {
  54.                 Console.WriteLine("User cancelled out of the open file dialog.");
  55.                 return;
  56.             }
  57.             if (dlg.FileName.Contains("CommonSnappableTypes"))
  58.                 Console.WriteLine("CommonSnappableTypes has no snap-ins!");
  59.             else if (!LoadExternalModule(dlg.FileName))
  60.                 Console.WriteLine("Nothing implements IAppFunctionality!");
  61.         }
  62.  
  63.         private static bool LoadExternalModule(string path)
  64.         {
  65.             bool foundSnapIn = false;
  66.             Assembly theSnapInAsm = null;
  67.  
  68.             try
  69.             {
  70.                 // Dynamically load the selected assembly.
  71.                 theSnapInAsm = Assembly.LoadFrom(path);
  72.             }
  73.             catch (Exception ex)
  74.             {
  75.                 Console.WriteLine($"An error occurred loading the snapin: {ex.Message}");
  76.                 return foundSnapIn;
  77.             }
  78.  
  79.             Type[] theTypes = theSnapInAsm.GetTypes();
  80.  
  81.             for (int i = 0; i < theTypes.Length; i++) {
  82.                 Type t;
  83.                 t = theTypes[i].GetInterface("ISortAndPrint");
  84.                 if (t != null) {
  85.                     foundSnapIn = true;
  86.                     object o = theSnapInAsm.CreateInstance(theTypes[i].FullName);
  87.                     loaded = o as ISortAndPrint;
  88.                 }
  89.             }
  90.             // Get all IAppFunctionality compatible classes in assembly.
  91.             /*var theClassTypes = from t in theSnapInAsm.GetTypes()
  92.                 where t.IsClass && (t.GetInterface("IAppFunctionality") != null)
  93.                 select t;
  94.                 */
  95.             // Now, create the object and call DoIt() method.
  96.             foreach (Type t in theClassTypes)
  97.             {
  98.                 foundSnapIn = true;
  99.                 // Use late binding to create the type.
  100.                 IAppFunctionality itfApp = (IAppFunctionality) theSnapInAsm.CreateInstance(t.FullName, true);
  101.                 itfApp?.DoIt();
  102.                 //lstLoadedSnapIns.Items.Add(t.FullName);
  103.  
  104.             }
  105.             return foundSnapIn;
  106.         }
  107.  
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement