C# Performance Tricks: How To Radically Speed Up Your Code
Finished an online program @ Udemy.
C# Performance Tricks: How To Radically Speed Up Your Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
private static ClassCreator GetClassCreator (string typeName) { // get delegate from dictionary if (ClassCreators.ContainsKey (typeName)) return ClassCreators [typeName]; // get the default constructor of the type Type t = Type.GetType (typeName); ConstructorInfo ctor = t.GetConstructor (new Type[0]); // create a new dynamic method that constructs and returns the type string methodName = t.Name + "Ctor"; DynamicMethod dm = new DynamicMethod (methodName, t, new Type[0], typeof(object).Module); ILGenerator lgen = dm.GetILGenerator (); lgen.Emit (OpCodes.Newobj, ctor); lgen.Emit (OpCodes.Ret); // add delegate to dictionary and return ClassCreator creator = (ClassCreator)dm.CreateDelegate (typeof(ClassCreator)); ClassCreators.Add (typeName, creator); return creator; } |