Generics at Runtime
Here is a little test code that demonstrates how to use Generics at runtime. The code has two tests. The first test shows how to create a Generic Class at runtime. The second shows how to call a Generic method. Keep in mind that when using generics at runtime, there will be a reflection performance hit.
~ Paul
using System; using System.Collections.ObjectModel; using Microsoft.VisualStudio.QualityTools.UnitTesting.Framework; using System.Reflection; using System.Diagnostics; namespace GenericTest { [TestClass] public class GenericTest { [TestMethod] public void CreateGenericAtRuntime() { Stopwatch watch = Stopwatch.StartNew(); // define the generic class at runtime Type genericType = typeof(Collection<>).MakeGenericType(typeof(DateTime)); // create an instance of the generic type object instance = Activator.CreateInstance(genericType); watch.Stop();Console.WriteLine(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Dynamic Create Time: {0} ms</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, watch.Elapsed.TotalMilliseconds); Assert.IsNotNull(instance, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">instance is Null</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); Assert.IsTrue(instance </span><span style="COLOR: #0000ff">is</span><span style="COLOR: #000000"> Collection</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">DateTime</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">); Collection</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">DateTime</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"> dates </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> instance </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> Collection</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">DateTime</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">; Assert.IsNotNull(dates, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">dates is Null</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); watch </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> Stopwatch.StartNew(); Collection</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">DateTime</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"> d </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> Collection</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">DateTime</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">(); watch.Stop(); Console.WriteLine(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Normal Create Time: {0} ms</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, watch.Elapsed.TotalMilliseconds); } </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> Collection</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"> GetCollection</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">() { </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> Collection</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">(); } [TestMethod()] </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> CallGenericMethodAtRuntime() { Stopwatch watch </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> Stopwatch.StartNew(); MethodInfo methodInfo </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">typeof</span><span style="COLOR: #000000">(GenericTest).GetMethod(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">GetCollection</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); MethodInfo genericInfo </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> methodInfo.MakeGenericMethod(</span><span style="COLOR: #0000ff">typeof</span><span style="COLOR: #000000">(DateTime)); </span><span style="COLOR: #0000ff">object</span><span style="COLOR: #000000"> instance </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> genericInfo.Invoke(</span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">, </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">); watch.Stop(); Console.WriteLine(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Dynamic Invoke Time: {0} ms</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, watch.Elapsed.TotalMilliseconds); Assert.IsNotNull(instance, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">instance is Null</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); Assert.IsTrue(instance </span><span style="COLOR: #0000ff">is</span><span style="COLOR: #000000"> Collection</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">DateTime</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">); Collection</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">DateTime</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"> dates </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> instance </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> Collection</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">DateTime</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">; Assert.IsNotNull(dates, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">dates is Null</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">); watch </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> Stopwatch.StartNew(); Collection</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">DateTime</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"> d </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.GetCollection</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">DateTime</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">(); watch.Stop(); Console.WriteLine(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Normal Invoke Time: {0} ms</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, watch.Elapsed.TotalMilliseconds); } }
}