Creating an instance of an open generic type without specifying a type argument
My fellow Roslyn tester @vreshetnikov has found not one, but two ways to do the impossible:
1:
using System;
class Program
{
static void Main()
{
var open = Enum.ToObject(typeof(C<>.E), 0);
Console.WriteLine(open.GetType());
}
}
class C<T>
{
public enum E { }
}
2:
using System;<br> <br>class Program<br>{<br> static void Main()<br> {<br> Action<C<int>.E> a = M;<br> var open = a.Method.GetGenericMethodDefinition().GetParameters()[0].DefaultValue;<br> Console.WriteLine(open.GetType());<br> }<br> <br> static void M<T>(C<T>.E e = 0) { }<br>}<br> <br>class C<T><br>{<br> public enum E { }<br>}