สร้าง class แบบ dynamic
กันยายน 2nd, 2009
using System; using System.Diagnostics; class Foo { } static class Program { static void Main() { Type type = typeof(Foo); const int count = 5000000;
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);RunTestWithActivator(type, count);
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);typeof(Program).GetMethod(“RunTestWithGenerics”). MakeGenericMethod(type).Invoke(null, new object[] { count }); } public static void RunTestWithActivator(Type type, int count) { Stopwatch watch = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { object obj = Activator.CreateInstance(type); } watch.Stop(); Console.WriteLine(“With Activator: {0}”, watch.ElapsedMilliseconds); } public static void RunTestWithGenerics<T>(int count) where T : new() { Stopwatch watch = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { T t = new T(); } watch.Stop(); Console.WriteLine(“With generics: {0}”, watch.ElapsedMilliseconds); } }
ความเห็นล่าสุด