shadowndacorner

c# fast generic initializer update

Aug 9th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Linq.Expressions;
  4. using System.Collections.Generic;
  5.  
  6. public static class FastActivator{
  7.     private static bool Initialized = false;
  8.     private static bool _isCreatingCache = false;
  9.     private static Dictionary<Type, Func<object>> Cache;
  10.  
  11.     public static void CreateCache()
  12.     {
  13.         if (Initialized)
  14.             return;
  15.  
  16.         Cache = new Dictionary<Type, Func<object>>();
  17.         _isCreatingCache = true;
  18.         var thr = new Thread(()=>
  19.         {
  20.             foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
  21.             {
  22.                 foreach(var t in asm.GetTypes())
  23.                 {
  24.                     if (t.GetConstructor(Type.EmptyTypes) != null && !t.ContainsGenericParameters)
  25.                     {
  26.                         Cache[t] = Expression.Lambda<Func<object>>(Expression.New(t)).Compile();
  27.                     }
  28.                 }
  29.             }
  30.             _isCreatingCache = false;
  31.             Initialized = true;
  32.         });
  33.         thr.Start();
  34.     }
  35.  
  36.     public static object CreateInstance(Type t)
  37.     {
  38.         if (!Initialized)
  39.         {
  40.             if (!_isCreatingCache)
  41.                 CreateCache();
  42.         }
  43.  
  44.         if (!Cache.ContainsKey(t))
  45.         {
  46.             var shouldUnInit = (t.GetConstructor(Type.EmptyTypes) == null);
  47.             if (shouldUnInit)
  48.             {
  49.                 return System.Runtime.Serialization.FormatterServices.GetUninitializedObject(t);
  50.             }
  51.             else
  52.             {
  53.                 return Activator.CreateInstance(t);
  54.             }
  55.         }
  56.         return Cache[t]();
  57.     }
  58.  
  59.     public static T CreateInstance<T>()
  60.     {
  61.         if (!Initialized)
  62.         {
  63.             if (!_isCreatingCache)
  64.                 CreateCache();
  65.         }
  66.  
  67.         var t = typeof(T);
  68.         if (!Cache.ContainsKey(t))
  69.         {
  70.             var shouldUnInit = (t.GetConstructor(Type.EmptyTypes) == null);
  71.             if (shouldUnInit)
  72.             {
  73.                 return (T)System.Runtime.Serialization.FormatterServices.GetUninitializedObject(t);
  74.             }
  75.             else
  76.             {
  77.                 return Activator.CreateInstance<T>();
  78.             }
  79.         }
  80.         return (T)Cache[t]();
  81.     }
  82.  
  83.     public static Array CreateArray(Type type, int len)
  84.     {
  85.         return Activator.CreateInstance(type, new object[] { len }) as Array;
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment