// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Reflection; using System.Runtime.CompilerServices; public class TestTrick { [LoaderOptimization(LoaderOptimization.MultiDomainHost)] public static void Main(string[] args) { AppDomain d1 = AppDomain.CreateDomain("D1"); Assembly a1 = d1.Load("Singleton1"); Type t1 = a1.GetType("Singleton.SimpleSingleton"); object o1 = t1.GetProperty("Instance").GetValue(null); Console.WriteLine("{0} - {1}", o1.GetType().FullName, RuntimeHelpers.GetHashCode(o1)); AppDomain d2 = AppDomain.CreateDomain("D2"); Assembly a2 = d2.Load("Singleton2"); Type t2 = a2.GetType("Singleton.SimpleSingleton"); object o2 = t2.GetProperty("Instance").GetValue(null); Console.WriteLine("{0} - {1}", o2.GetType().FullName, RuntimeHelpers.GetHashCode(o2)); AppDomain d3 = AppDomain.CreateDomain("D3"); Assembly a3 = d3.Load("Singleton3"); Type t3 = a3.GetType("Singleton.SimpleSingleton"); object o3 = t3.GetProperty("Instance").GetValue(null); Console.WriteLine("{0} - {1}", o3.GetType().FullName, RuntimeHelpers.GetHashCode(o3)); } }