Created
November 26, 2012 22:26
-
-
Save DevJohnC/4151070 to your computer and use it in GitHub Desktop.
Adjutant extension loading
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
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Windows.Forms; | |
| namespace FragLabs.Adjutant.API.Server.Extensions | |
| { | |
| /// <summary> | |
| /// Extension loader. | |
| /// </summary> | |
| public class Loader | |
| { | |
| static int domainCount = 0; | |
| AppDomain remoteDomain = null; | |
| public App[] Apps { get; private set; } | |
| public string Filename { get; private set; } | |
| public string Directory { get; private set; } | |
| public void Load(string assembly) | |
| { | |
| var fi = new System.IO.FileInfo(assembly); | |
| Filename = fi.FullName; | |
| Directory = fi.Directory.FullName; | |
| System.Security.Policy.Evidence evidence = new System.Security.Policy.Evidence(AppDomain.CurrentDomain.Evidence); | |
| AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation; | |
| setup.PrivateBinPath = fi.Directory.FullName; | |
| setup.ApplicationBase = fi.Directory.FullName; | |
| remoteDomain = AppDomain.CreateDomain("ModDomain" + (++domainCount).ToString(), evidence, setup); | |
| var proxy = (LoaderProxy)remoteDomain.CreateInstanceAndUnwrap(typeof(Loader).Assembly.FullName, typeof(LoaderProxy).FullName); | |
| proxy.Load(assembly); | |
| Apps = proxy.GetApps(); | |
| } | |
| public void Unload() | |
| { | |
| AppDomain.Unload(remoteDomain); | |
| } | |
| } | |
| public class LoaderProxy : MarshalByRefObject | |
| { | |
| /// <summary> | |
| /// Static loaded assembly, different for each application domain. | |
| /// </summary> | |
| public static System.Reflection.Assembly LoadedAssembly = null; | |
| public void Load(string assembly) | |
| { | |
| LoadedAssembly = System.Reflection.Assembly.LoadFile(assembly); | |
| } | |
| public App[] GetApps() | |
| { | |
| List<App> ret = new List<App>(); | |
| var appType = typeof(App); | |
| foreach (var type in LoadedAssembly.GetTypes()) | |
| { | |
| if (type.IsSubclassOf(appType)) | |
| { | |
| var instance = (App)Activator.CreateInstance(type); | |
| ret.Add(instance); | |
| } | |
| } | |
| return ret.ToArray(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment