public void TestCopyFromAndTo() { using (var item = new ExternalCacheItem()) { var obj = new TestClass { Prop1 = "P1", Prop2 = "P2", Prop3 = "P3", Prop4 = -1 }; item.CopyFrom(obj); Assert.AreEqual("P1", item.Container.Prop1); Assert.AreEqual("P2", item.Container.Prop2); Assert.AreEqual("P3", item.Container.Prop3); Assert.AreEqual(-1, item.Container.Prop4); obj = new TestClass(); Assert.AreEqual(null, obj.Prop1); Assert.AreEqual(null, obj.Prop2); Assert.AreEqual(null, obj.Prop3); Assert.AreEqual(0, obj.Prop4); item.CopyTo(obj); Assert.AreEqual("P1", obj.Prop1); Assert.AreEqual("P2", obj.Prop2); Assert.AreEqual("P3", obj.Prop3); Assert.AreEqual(-1, obj.Prop4); using (var item2 = new ExternalCacheItem()) { item.CopyTo(item2._container); Assert.AreEqual(4, item2.Container.Count); Assert.AreEqual("P1", item2.Container.Prop1); Assert.AreEqual("P2", item2.Container.Prop2); Assert.AreEqual("P3", item2.Container.Prop3); Assert.AreEqual(-1, item2.Container.Prop4); using (var item3 = new ExternalCacheItem()) { item3.CopyFrom(item2._container); Assert.AreEqual(4, item3.Container.Count); Assert.AreEqual("P1", item3.Container.Prop1); Assert.AreEqual("P2", item3.Container.Prop2); Assert.AreEqual("P3", item3.Container.Prop3); Assert.AreEqual(-1, item3.Container.Prop4); } } } }
static void Main() { const int loops = 100000; const int threadCount = 4; NumbersAdder adder = new NumbersAdder(); // This object now created within the context of COM Surrogate (32 bit) process Console.WriteLine("10 + 12 = " + adder.add(10, 12)); // This call marshaled to COM Surrogate process hosting a copy of NumberOperators.dll NumbersSubstracter substracter = new NumbersSubstracter(); // This object created within current AppDomain as a normal .NET assembly Console.WriteLine("10 - 12 = " + substracter.sub(10, 12)); // Normal call to .NET object IntPair pair = new IntPair() { a = 10, b = 12 }; Console.WriteLine("pair.a = 10, pair.b = 12. adder.add(pair) = " + adder.add(pair)); pair = adder.buildPair(5, 10); Console.WriteLine("Built pair in COM Server. pair.a = " + pair.a + ". pair.b = " + pair.b); pair.Clear(); Console.WriteLine("Called pair.Clear()"); Console.WriteLine("Built pair in COM Server. pair.a = " + pair.a + ". pair.b = " + pair.b); var initialTicks = Environment.TickCount; for (var i = 0; i < loops; i++) { adder.add(10, 2); } Console.WriteLine("Requests per second (Single threaded): " + (loops / ((float)(Environment.TickCount - initialTicks) / 1000))); initialTicks = Environment.TickCount; var threads = new Thread[threadCount]; for (var i = 0; i < threadCount; i++) { (threads[i] = new Thread((innerAdder => { for (var j = 0; j < loops; j++) { ((NumbersAdder)innerAdder).add(10, 2); } }))).Start(new NumbersAdder()); } foreach (var thread in threads) { thread.Join(); } Console.WriteLine($"Aggregate requests per second ({threadCount} threads): " + (int)(threadCount * loops / ((float)(Environment.TickCount - initialTicks) / 1000))); Console.WriteLine($"Per thread requests per second ({threadCount} threads): " + (int)(loops / ((float)(Environment.TickCount - initialTicks) / 1000))); initialTicks = Environment.TickCount; var adderi = (INumbersAdder)adder; for (var i = 0; i < loops; i++) { adderi.add(10, 2); } Console.WriteLine("Requests per second (Single threaded using interface): " + (loops / ((float)(Environment.TickCount - initialTicks) / 1000))); var externalCache = new ExternalCache(); // externalCache.Select("CacheTest"); externalCache.Add("test", 1); var item = new ExternalCacheItem(); item["Test"] = "hello"; externalCache.Add("test 2", item, new TimeSpan(5000)); var retrieved = (ExternalCacheItem)externalCache.Get("test 2"); Console.WriteLine(retrieved.Container.Test); Thread.Sleep(10000); retrieved = (ExternalCacheItem)externalCache.Get("test 2"); Console.WriteLine("Retrieved = null:" + (retrieved == null)); var tester = new Tester { Val = "Value 1" }; item = new ExternalCacheItem(); item.CopyFrom(tester); externalCache.Add("test 2", item); item = (ExternalCacheItem)externalCache.Get("test 2"); tester = new Tester(); item.CopyTo(tester); Console.WriteLine(tester.Val); Console.WriteLine("Press any key to finish"); Console.ReadLine(); }