static void Main(string[] args) { //Marshalling(); //FieldTest.FieldTestMethod(); //SpeedTest(); //LoadAssembly(); //NewMethod(); string addInDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); var addInAssemblies = Directory.EnumerateFiles(addInDir, "*.dll"); var addInTypes = from file in addInAssemblies let assembly = Assembly.Load(file) from t in assembly.ExportedTypes where t.IsClass && typeof(IAddIn).GetTypeInfo().IsAssignableFrom(t.GetTypeInfo()) select t; foreach (var t in addInTypes) { IAddIn ai = (IAddIn)Activator.CreateInstance(t); Console.WriteLine(ai.DoSomething(5)); } Console.ReadKey(); }
public static void Main() { // Find the directory that contains the Host exe String AddInDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); // Assume AddIn assemblies are in same directory as host's EXE file var AddInAssemblies = Directory.EnumerateFiles(AddInDir, "*.dll"); // Create a collection of Add-In Types usable by the host var AddInTypes = from file in AddInAssemblies let assembly = Assembly.Load(file) from t in assembly.ExportedTypes // Publicly-exported types // Type is usable if it is a class that implements IAddIn where t.IsClass && typeof(IAddIn).GetTypeInfo().IsAssignableFrom(t.GetTypeInfo()) select t; // Initialization complete: the host has discovered the usable Add-Ins // Here's how the host can construct Add-In objects and use them foreach (Type t in AddInTypes) { IAddIn ai = (IAddIn)Activator.CreateInstance(t); Console.WriteLine(ai.DoSomething(5)); Console.ReadKey(); } }
static void Main(string[] args) { string AddInDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); var AddInAssemblies = Directory.EnumerateFiles(AddInDir, "*.dll"); var AddInTypes = from file in AddInAssemblies let assembly = Assembly.Load(file) from t in assembly.ExportedTypes where t.IsClass && typeof(IAddIn).GetTypeInfo().IsAssignableFrom(t.GetTypeInfo()) select t; foreach (Type t in AddInTypes) { IAddIn ai = (IAddIn)Activator.CreateInstance(t); Console.WriteLine(ai.DoSomething(5)); } }
public static void Main() { // Find the directory that contains the Host exe String AddInDir = Path.GetDirectoryName( Assembly.GetEntryAssembly().Location); // Assume AddIn assemblies are in same directory as host's EXE file String[] AddInAssemblies = Directory.GetFiles(AddInDir, "*.dll"); // Create a collection of usable Add-In Types List <Type> AddInTypes = new List <Type>(); // Load Add-In assemblies; discover which types are usable by the host foreach (String file in AddInAssemblies) { Assembly AddInAssembly = Assembly.LoadFrom(file); // Examine each publicly-exported type foreach (Type t in AddInAssembly.GetExportedTypes()) { // If the type is a class that implements the IAddIn // interface, then the type is usable by the host if (t.IsClass && typeof(IAddIn).IsAssignableFrom(t)) { AddInTypes.Add(t); } } } // Initialization complete: the host has discovered the usable Add-Ins // Here's how the host can construct Add-In objects and use them foreach (Type t in AddInTypes) { IAddIn ai = (IAddIn)Activator.CreateInstance(t); Console.WriteLine(ai.DoSomething(5)); } }