#副標=.NET Framework 初旅(4/4) #大標= Assembly進階應用(中) #作者=文/蔡孟哲    ----------box程式---------- 1.using System; 2.using System.Reflection; 3. 4.namespace AppDomain 5.{ 6. /// 7. /// Summary description for Class1. 8. /// 9. class MyAppDomain 10. { 11. /// 12. /// The main entry point for the application. 13. /// 14. [STAThread] 15. static void Main(string[] args) 16. { 17. System.AppDomain objDomain = 18. System.AppDomain.CreateDomain("MyDomain"); 19. Console.WriteLine("Runtime Host Created Domain : {0}", 20. System.AppDomain.CurrentDomain.FriendlyName); 21. Console.WriteLine("My Domain : {0}", 22. objDomain.FriendlyName); 23. Console.WriteLine("Application Name : {0}", 24. objDomain.SetupInformation.ApplicationName); 25. Console.WriteLine("Application Path : {0}", 26. objDomain.SetupInformation.ApplicationBase); 27. Console.WriteLine("Application Configuration File : {0}", 28. objDomain.SetupInformation.ConfigurationFile); 29. Console.WriteLine("---------Before----------"); 30. Console.WriteLine("Unload Application Domain"); 31. System.AppDomain.Unload(objDomain); 32. try 33. { 34. Console.WriteLine("---------After----------"); 35. Console.WriteLine("Runtime Host Created Domain : {0}", 36. System.AppDomain.CurrentDomain.FriendlyName); 37. Console.WriteLine("My Domain : {0}", 38. objDomain.FriendlyName); 39. } 40. catch(System.AppDomainUnloadedException e) 41. { 42. Console.WriteLine(e.Message); 43. } 44. } 45. } 46.}    ----------end----------    ----------box程式---------- Runtime Host Created Domain : AppDomain.exe My Domain : MyDomain Application Name : Application Path : D:\Documents and Settings\Administrator\My Documents\ Visual Studio Projects\AppDomain\bin\Debug\ Application Configuration File : D:\Documents and Settings\Administrator\ My Documents\Visual Studio Projects\AppDomain\bin\Debug\AppDomain.exe.config ---------Before---------- Unload Application Domain ----------After----------- Runtime Host Created Domain : AppDomain.exe The target application domain has been unloaded. ----------end----------        ----------box程式---------- 1.using System; 2.using System.Reflection; 3. 4.namespace AppDomain 5.{ 6. /// 7. /// Summary description for Class1. 8. /// 9. class MyAppDomain 10. { 11. /// 12. /// The main entry point for the application. 13. /// 14. [STAThread] 15. static void Main(string[] args) 16. { 17. System.AppDomainSetup objSetup = new System.AppDomainSetup(); 18. objSetup.ApplicationName = "MyAppDomain"; 19. objSetup.ApplicationBase = "C:\\MyAppDir"; 20. objSetup.ConfigurationFile = "C:\\MyAppDir\\MyApp.Config"; 21. System.AppDomain objDomain = 22. System.AppDomain.CreateDomain("MyDomain",null,objSetup); 23. Console.WriteLine("Runtime Host Created Domain : {0}", 24. System.AppDomain.CurrentDomain.FriendlyName); 25. Console.WriteLine("My Domain : {0}", 26. objDomain.FriendlyName); 27. Console.WriteLine("Application Name : {0}", 28. objDomain.SetupInformation.ApplicationName); 29. Console.WriteLine("Application Path : {0}", 30. objDomain.SetupInformation.ApplicationBase); 31. Console.WriteLine("Application Configuration File : {0}", 32. objDomain.SetupInformation.ConfigurationFile); 33. Console.WriteLine("---------Before----------"); 34. Console.WriteLine("Unload Application Domain"); 35. System.AppDomain.Unload(objDomain); 36. try 37. { 38. Console.WriteLine("---------After----------"); 39. Console.WriteLine("Runtime Host Created Domain : {0}", 40. System.AppDomain.CurrentDomain.FriendlyName); 41. Console.WriteLine("My Domain : {0}", 42. objDomain.FriendlyName); 43. } 44. catch(System.AppDomainUnloadedException e) 45. { 46. Console.WriteLine(e.Message); 47. } 48. } 49. } 50.}    ----------end----------       ----------box程式---------- Runtime Host Created Domain : AppDomain.exe My Domain : MyDomain Application Name : MyAppDomain Application Path : C:\MyAppDir Application Configuration File : C:\MyAppDir\MyApp.Config ---------Before---------- Unload Application Domain ---------After---------- Runtime Host Created Domain : AppDomain.exe The target application domain has been unloaded. ----------end----------    ----------box程式---------- 1.using System; 2.using System.Reflection; 3. 4.namespace AppDomain 5.{ 6. /// 7. /// Summary description for Class1. 8. /// 9. class MyAppDomain 10. { 11. /// 12. /// The main entry point for the application. 13. /// 14. [STAThread] 15. static void Main(string[] args) 16. { 17. Console.WriteLine("Loading Assembly by 18. System.AppDomain.Load(\"Assembly Name\"))"); 19. try 20. { 21. System.AppDomain objDomain = 22. System.AppDomain.CreateDomain("MyDomain"); 23. System.Reflection.Assembly objAsm = 24. objDomain.Load("MyObjectLib"); 25. Type objType = objAsm.GetType("MyObjectLib.MyObject"); 26. if (objType == null) 27. { 28. Console.WriteLine("Open MyObject Failed!"); 29. return; 30. } 31. else 32. { 33. Console.WriteLine("Open MyObject Succeeded!"); 34. } 35. System.Reflection.MethodInfo set_Asset_Method = 36. objType.GetMethod("set_Asset"); 37. System.Reflection.MethodInfo get_Asset_Method = 38. objType.GetMethod("get_Asset"); 39. System.Reflection.MethodInfo SpendMoney_Method = 40. objType.GetMethod("SpendMoney"); 41. System.Collections.ArrayList paramList = 42. new System.Collections.ArrayList(); 43. paramList.Clear(); 44. paramList.Add(1000000); 45. object objTarget = System.Activator.CreateInstance(objType); 46. set_Asset_Method.Invoke(objTarget,paramList.ToArray()); 47. paramList.Clear(); 48. object theRet = 49. get_Asset_Method.Invoke(objTarget,paramList.ToArray()); 50. Console.WriteLine("Current Asset : {0}", theRet); 51. paramList.Clear(); 52. paramList.Add(520000); 53. paramList.Add("Ford Tierra"); 54. SpendMoney_Method.Invoke(objTarget,paramList.ToArray()); 55. paramList.Clear(); 56. theRet = 57. get_Asset_Method.Invoke(objTarget,paramList.ToArray()); 58. Console.WriteLine("Current Asset : {0}", theRet); 59. System.AppDomain.Unload(objDomain); 60. } 61. catch(System.IO.FileNotFoundException fne) 62. { 63. Console.WriteLine(fne.Message); 64. } 65. catch(System.AppDomainUnloadedException e) 66. { 67. Console.WriteLine(e.Message); 68. } 69. } 70.}    ----------end----------        ----------box程式---------- Loading Assembly by System.AppDomain.Load("Assembly Name")) Open MyObject Succeeded! Current Asset : 1000000 Spend 520000 NT$ for Ford Tierra Current Asset : 480000 ----------end----------        ----------box程式---------- 1.System.AppDomain objDomain = 2. System.AppDomain.CreateDomain("MyDomain"); 3.System.Reflection.Assembly objAsm = 4. System.Reflection.Assembly.LoadFrom("MyObjectLib.dll"); 5.objDomain.Load(objAsm.GetName()); ----------end----------    ----------box程式---------- 1.System.AppDomain objDomain = 2. System.AppDomain.CreateDomain("MyDomain"); 3.System.Reflection.Assembly objAsm = 4. System.Reflection.Assembly.Load ("MyObjectLib "); 5.objDomain.Load(objAsm.GetName()); ----------end----------