Grax32

Untitled

Sep 13th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     using System;
  10.     using System.Diagnostics;
  11.     using System.IO;
  12.     using System.Linq.Expressions;
  13.     using System.Reflection;
  14.  
  15.     public class Program
  16.     {
  17.         private static void Main(string[] args)
  18.         {
  19.             var m = new Model();
  20.             var s = new MemoryStream();
  21.  
  22.             var sw = Stopwatch.StartNew();
  23.             for (var i = 0; i < 1000000; i++)
  24.             {
  25.                 Test.Write(s, m);
  26.             }
  27.             Console.WriteLine("{0} ms", sw.ElapsedMilliseconds);
  28.  
  29.             var ex = new ExplicitHandler<Model>();
  30.             sw.Restart();
  31.             for (var i = 0; i < 1000000; i++)
  32.             {
  33.                 ex.Write(s, m);
  34.             }
  35.             Console.WriteLine("{0} ms", sw.ElapsedMilliseconds);
  36.         }
  37.     }
  38.  
  39.     public class HandlerFactory
  40.     {
  41.         public static IHandler<T> Create<T>()
  42.         {
  43.             return new ExplicitHandler<T>();
  44.         }
  45.     }
  46.  
  47.     public interface IHandler<T>
  48.     {
  49.         T Read(Stream s);
  50.  
  51.         void Write(Stream s, T v);
  52.     }
  53.  
  54.     public interface IHandler
  55.     {
  56.         object Read(Stream s);
  57.         void Write(Stream s, object v);
  58.     }
  59.  
  60.     public class Model
  61.     {
  62.     }
  63.  
  64.     public class ExplicitHandler<T> : IHandler<T>, IHandler
  65.     {
  66.         public T Read(Stream s)
  67.         {
  68.             return default(T);
  69.         }
  70.  
  71.         public void Write(Stream s, T v)
  72.         {
  73.  
  74.         }
  75.  
  76.         void IHandler.Write(Stream s, object v)
  77.         {
  78.             Write(s, (T)v);
  79.         }
  80.  
  81.         object IHandler.Read(Stream s)
  82.         {
  83.             return Read(s);
  84.         }
  85.     }
  86.  
  87.     public class Test
  88.     {
  89.         static Dictionary<Type, IHandler> delegates = new Dictionary<Type, IHandler>();
  90.  
  91.         public static void Write(MemoryStream s, object value)
  92.         {
  93.             IHandler handler;
  94.  
  95.             var type = value.GetType();
  96.             if (!delegates.TryGetValue(type, out handler))
  97.             {
  98.                 handler = (IHandler)typeof(HandlerFactory).GetMethod(nameof(HandlerFactory.Create)).MakeGenericMethod(type).Invoke(null, null);
  99.                 delegates[type] = handler;
  100.             }
  101.  
  102.             handler.Write(s, value);
  103.         }
  104.     }
  105. }
Add Comment
Please, Sign In to add comment