dotnet/runtime

Reflection on enums creates instances of open generic types

Open

#7 976 ouverte le 28 avr. 2017

Voir sur GitHub
 (16 commentaires) (3 réactions) (0 assignés)C# (5 445 forks)batch import
area-System.Reflectionbughelp wanted

Métriques du dépôt

Stars
 (17 886 stars)
Métriques de merge PR
 (Merge moyen 12j 11h) (661 PRs mergées en 30 j)

Description

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
        }
    }
}

Guide contributeur