Last active
December 19, 2016 20:49
-
-
Save XSockets/7102625 to your computer and use it in GitHub Desktop.
Rutime recomposition - The XSockets pluginframework can now load assemblies at runtime.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Zoo : IZoo | |
{ | |
public IList<IAnimal> Animals { get; set; } | |
} | |
public class Frog : IAnimal | |
{ | |
public string Say() | |
{ | |
return "Riiiibbitt"; | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//First of all tell the framework to also look in *.exe files and not only *.dll | |
//This since we have a sample IZoo and IAnimal class in this ConsoleApplication (.exe) | |
Composable.AddPluginFilter("*.exe"); | |
//Get the Zoo | |
var zoo = Composable.GetExport<IZoo>(); | |
//Display the animals... Will only find the Frog here... | |
foreach (var animal in zoo.Animals) | |
{ | |
Console.WriteLine(animal.Say()); | |
} | |
Console.WriteLine("Hit enter to load an additional assembly..."); | |
Console.ReadLine(); | |
//Load another assembly containing some more classes that implements IAnimal interface | |
Composable.LoadAssembly(@"C:\PathToAssembly.dll"); | |
//Call recompose to get the new Exports/Imports that we might be new after the LoadAssembly call | |
Composable.Recompose(); | |
//Get the Zoo once more... | |
zoo = Composable.GetExport<IZoo>(); | |
//Displat the animals... Will now contain 2 additional animals (see output sample below) | |
foreach (var animal in zoo.Animals) | |
{ | |
Console.WriteLine(animal.Say()); | |
} | |
Console.WriteLine("Hit enter to quit..."); | |
Console.ReadLine(); | |
} | |
} | |
/* OUTPUT | |
Riiiibbitt | |
Hit enter to load an additional assembly... | |
Riiiibbitt | |
Grr | |
Yeeehaaa | |
Hit enter to quit... | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The interfaces export the correct inteface, but can also import interfaces on properties. | |
//The implementing class does not need to do any imports/exports. It will be taken care of.... | |
[Export(typeof(IZoo))] | |
public interface IZoo | |
{ | |
[ImportMany(typeof(IAnimal))] | |
IList<IAnimal> Animals { get; set; } | |
} | |
[Export(typeof(IAnimal))] | |
public interface IAnimal | |
{ | |
string Say(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This assembly is loaded at runtime above... | |
public class Lion : IAnimal | |
{ | |
public string Say() | |
{ | |
return "Grr"; | |
} | |
} | |
public class Donkey : IAnimal | |
{ | |
public string Say() | |
{ | |
return "Yeeehaaa"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment