AppDomain能夠理解爲一個獨立的沙箱,當有獨立的第靜態對象在appDomain中被訪問時,會在appDomain中產生獨立的內存對象。好比appDomain1 appDomain2同時對 靜態對象A有引用時,appDomain1.A與appDomain2.A是兩個對象。app
1.dom
靜態對象屬性spa
public class StaticModel { public static int IndexTest; }
2. code
public class myClass { public int index; public void Invoke() { using (TestProctectedCodeInvoke tc = new TestProctectedCodeInvoke()) { LogHelper.Log("index:" + index + "----------Invoke thread .id:" + Thread.CurrentThread.ManagedThreadId, "ddd", 111); string saa = tc.test(); Console.WriteLine(saa); } } }
public static void Main(string[] args) { LogHelper.Log("Main start", "ffffffffff", 111); int i = 0; try { while (i < 11) { i++; myClass mc = new myClass(); mc.index = i; LogHelper.Log("Main thread index: 【" + i + "】", "ffffffffff", 111); Thread t = new Thread(mc.Invoke); t.Start(); Thread.Sleep(100); Console.WriteLine(i); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } LogHelper.Log("Main end", "ffffffffff", 111); Console.ReadLine(); }
class TestProctectedCodeInvoke: IDisposable { public string test() { string dllpath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory + "ClassLibrary1.dll"); AssemblyDomain ad = new AssemblyDomain(); object s = ad.Invoke(dllpath, "ClassLibrary1.Class1", "GetName"); return s.ToString(); } public void Dispose() { Console.WriteLine("Dispose"); } ~TestProctectedCodeInvoke() { Console.WriteLine("TestProctectedCodeInvoke .xigou"); } }
3. 輸出結果對象