ManagedDll ãåçã«èªã¿è¾¼ããããã
æµæ´¾ã¯2ã¤ã
Assembly 使ã£ã¦ãåçã«ãªã³ã¯ããæ¹æ³
http://athomejp.com/goldfish/vcs/assemblycall.asp
AppDomain 使ã£ã¦ãåçã«ãã¼ãããæ¹æ³
http://d.hatena.ne.jp/akiramei/20071111/1194786373
åè
ã®ã»ããç°¡æã ãã©ãå¾è
ã¯ã¢ã³ãã¼ãã§ãããããªã
ãã«ãDLLãªãè¿·ããå¾è
ã ãªã
è¦ãã人ã ããµã³ãã«
Assembly ã¯ã©ã¹ã«ãããã¼ã
Assembly ã¯ã©ã¹ã§ DLL ã決ãæã¡ã§èªã¿è¾¼ã¿ããã¼ããã¦ããæ¹æ³ãã¤ã³ã¹ã¿ã³ã¹ã¯Activatorã§ç¡çããçæããã
ã¤ã³ã¿ã¼ãã§ã¼ã¹ç¨æãã¨ãã¨ãã¢ã¯ã»ã¹ã容æã
ãããã¤ã³ã¿ã¼ãã§ã¼ã¹ãç¨æããªãã®ã§ããã°ãType.GetMethod ããType.GetProperty ã§MethodInfo ã¨ã PropertyInfo çµç±ã§ãã«ããã«ãããªãããããªãã
public interface IHello { string getDllMessage(); }
ã§ããããç¶æ¿ãã¦ã¯ã©ã¹(DLL)ä½æ
namespace Hello { public class SampleHello : DinamicDLLLoad.IHello { public string getDllMessage() { return "Mr,Azalea"; } } }
ããã¦ããããåçã«èªã¿è¾¼ãã³ã¼ã
static void Main(string[] args) { while (true) { System.Threading.Thread.Sleep(500); if (System.IO.File.Exists("Hello.dll")) break; Console.WriteLine("Cannot find Hello.dll"); } try { /* DLL ãæå®ãã¦èªã¿è¾¼ã */ Assembly assembly = Assembly.LoadFrom("Hello.dll"); Module module = assembly.GetModule("Hello.dll"); Type type = module.GetType("Hello.SampleHello"); /* ã¤ã³ã¹ã¿ã³ã¹ãä½æãã */ IHello hello = Activator.CreateInstance(type) as IHello; Console.WriteLine("Hello " + hello.getDllMessage()); } catch (Exception) { } Console.WriteLine("Push any key to exit;"); Console.ReadLine(); }
AppDomain 使ã£ã¦èªã¿è¾¼ã
ãã£ã¡ã®å®è£
ã ã¨ãã¢ã³ãã¼ããå¯è½ã«ãªãã
æ±ãçã«ã¯åããã»ã¹(ã¿ãããªãã®)ãç«ã¦ã¦ãã½ã³ããããã«åçã«ã¯ã©ã¹ã¤ã³ã¹ã¿ã³ã¹ãæã£ã¦ããã
ã ãããããã»ã¹(Domain)ãæ»ãã°ãdll ãéæ¾ãããã¨ããã¯ã±ã
static void Main(string[] args) { while (true) { System.Threading.Thread.Sleep(500); if (System.IO.File.Exists("Hello.dll")) break; Console.WriteLine("Cannot find Hello.dll"); } AppDomainSetup setup = new AppDomainSetup(); setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; /* ã·ã£ãã¦ã³ãã¼ã§ dll ã使ãã®ã§ãéä¸åé¤OK */ setup.ShadowCopyFiles = "true"; AppDomain mainDomain = AppDomain.CreateDomain("SampleCode", null, setup); IHello hello = mainDomain.CreateInstanceAndUnwrap("Hello", "Hello.SampleHello") as IHello; Console.WriteLine("Push any key to exit;"); Console.ReadLine(); }