Skip to content

Instantly share code, notes, and snippets.

@kkozmic
Created September 22, 2010 12:03
Show Gist options
  • Save kkozmic/591560 to your computer and use it in GitHub Desktop.
Save kkozmic/591560 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using Castle.DynamicProxy;
namespace ConsoleApplication7
{
internal class Program
{
private static void Main(string[] args)
{
var generator = new ProxyGenerator();
var exceptions = new HashSet<Type>
{
typeof (TypedReference),
typeof (ArgIterator),
typeof (RuntimeArgumentHandle),
typeof (void)
};
var types =
typeof (object).Assembly.GetExportedTypes()
.Where(t => t.IsGenericTypeDefinition == false && exceptions.Contains(t) == false)
.ToArray();
Console.WriteLine("Experimenting with {0} types", types.Length);
var sw = Stopwatch.StartNew();
for (var i = 0; i < types.Length; i++)
{
var type = MakeGenericFoo(types[i]);
var instance = Instantiate(type, generator);
}
Console.WriteLine("Done: {0}", sw.Elapsed);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void Invoke(IList instance)
{
instance.Clear();
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static object Instantiate(Type type, ProxyGenerator generator)
{
return generator.CreateInterfaceProxyWithoutTarget(type);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static Type MakeGenericFoo(Type type)
{
return typeof (IFoo<>).MakeGenericType(type);
}
}
public interface IFoo<T>
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment