Created
October 4, 2018 21:12
-
-
Save kg/ec4641a74f77d17ed98e6a76a85febf9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Runtime.Serialization.Formatters.Binary; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace FormatterTest { | |
class Program { | |
public static AppDomain Domain; | |
public static void Main (string[] args) { | |
Domain = AppDomain.CreateDomain("TestDomain"); | |
Test("test"); | |
var dict = new Dictionary<string, string> { | |
{"a", "b" }, | |
{"c", "d" } | |
}; | |
Test(dict); | |
var kvpList = dict.ToList(); | |
Test((IReadOnlyList<KeyValuePair<string, string>>)kvpList); | |
if (Debugger.IsAttached) | |
Console.ReadLine(); | |
} | |
private static void Test<T> (IReceiver<T> receiver, T value) { | |
receiver.Receive(value); | |
var result = receiver.Return(value); | |
Dump("roundtrip", result); | |
} | |
public static void Test<T> (T value) { | |
Console.WriteLine("//// {0}", typeof(T).Name); | |
var localInstance = new ReceiverClass<T>(); | |
Test(localInstance, value); | |
Console.WriteLine("// BinaryFormatter"); | |
var formatterInstance = new FormatterReceiver<T>(); | |
Test(formatterInstance, value); | |
Console.WriteLine("// Remoting"); | |
var remoteInstance = (IReceiver<T>)Domain.CreateInstanceAndUnwrap( | |
localInstance.GetType().Assembly.FullName, localInstance.GetType().FullName | |
); | |
Test(remoteInstance, value); | |
} | |
public static void Dump (string label, object obj) { | |
var dict = obj as IDictionary; | |
var ie = obj as IEnumerable; | |
if (dict != null) { | |
Console.WriteLine("{0} -> dict[{1}] {{", label, dict.Count); | |
foreach (DictionaryEntry dent in dict) | |
Console.WriteLine("{0}, {1}", dent.Key, dent.Value); | |
Console.WriteLine("}"); | |
} else if (ie != null) { | |
Console.WriteLine("{0} -> enumerable[{1}] {{", label, ie.Cast<object>().Count()); | |
foreach (object elt in ie) | |
Console.WriteLine(elt); | |
Console.WriteLine("}"); | |
} else { | |
Console.WriteLine("{0} -> {1}", label, obj); | |
} | |
} | |
} | |
public interface IReceiver<T> { | |
void Receive (T value); | |
T Return (T value); | |
} | |
public class ReceiverClass<T> : MarshalByRefObject, IReceiver<T> { | |
public void Receive (T value) { | |
Program.Dump("receive", value); | |
} | |
public T Return (T value) { | |
return value; | |
} | |
} | |
public class FormatterReceiver<T> : IReceiver<T> { | |
public readonly BinaryFormatter Formatter = new BinaryFormatter(); | |
public void Receive (T value) { | |
var temp = Return(value); | |
Program.Dump("receive", temp); | |
} | |
public T Return (T value) { | |
using (var ms = new MemoryStream()) { | |
Formatter.Serialize(ms, value); | |
ms.Seek(0, SeekOrigin.Begin); | |
var result = Formatter.Deserialize(ms); | |
return (T)result; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment