vendredi 28 avril 2017

Static classes can be used as type arguments via reflection

When trying to use a static class as a type parameter, the C# compiler will throw an error:

var test = new List<Math>();

error CS0718: `System.Math': static classes cannot be used as generic arguments

This has been covered in these questions:

However, I just realized I can create the type via reflection, and the runtime won't complain:

var test = Activator.CreateInstance(typeof(List<>).MakeGenericType(typeof(Math)));

Am I right in concluding this is supported at the CLR level, but not at the language level?

Or is this a gray area in specifications, meaning I should refrain from using these generic constructed types?





Aucun commentaire:

Enregistrer un commentaire