dotnet/runtime
Auf GitHub ansehenReflection on enums creates instances of open generic types
Open
#7.976 geöffnet am 28. Apr. 2017
area-System.Reflectionbughelp wanted
Repository-Metriken
- Stars
- (17.886 Stars)
- PR-Merge-Metriken
- (Durchschn. Merge 12T 11h) (661 gemergte PRs in 30 T)
Beschreibung
From https://codeblog.jonskeet.uk/2017/04/26/surprise-creating-an-instance-of-an-open-generic-type/
using System;
using System.Reflection;
class Program
{
static void Main(string[] args)
{
object x = GetWeirdValue();
// This line prints True
Console.WriteLine(x.GetType().GetTypeInfo().IsGenericTypeDefinition);
}
static object GetWeirdValue() =>
typeof(Generic<>.GenericEnum).GetTypeInfo()
.GetDeclaredField("Foo")
.GetValue(null);
class Generic<T>
{
public enum GenericEnum
{
Foo = 0
}
}
}