Type.IsEnum Property in Portable Class Library
Asked Answered
A

1

17

I'm trying to code in a Portable Class Library using ASP.NET Core 1.0, the following instruction:

public static void WriteMessage<T>(T value)
{
    if (typeof(T).IsEnum)
    {
        Debug.Print("Is enum")
    }
    else
    {
        Debug.Print("Not Is enum")
    }
}

But this code does not compile because the compiler says that the property IsEnum is non present on Type.

Any suggestions?

Arlon answered 12/7, 2016 at 11:18 Comment(1)
documentation says : "If the current Type represents a type parameter in the definition of a generic type or generic method, this property always returns false." this may be relevant, tooFoehn
J
42

Some functionality from Type was moved to TypeInfo in .NET Core.

typeof(T).GetTypeInfo().IsEnum
Jacindajacinta answered 12/7, 2016 at 11:27 Comment(4)
Hi Gabriel, I try to use your code but the function GetTypeInfo() is not present on Type. what is wrong?Arlon
GetTypeInfo() is an extension method from IntrospectionExtensions. Add using System.Reflection to your class.Jacindajacinta
It's right!!! I forgot the "Using". Thank you Gabriel!! now it works :-)Arlon
This also worked for me. @Arlon mind marking as answer so it can float to the top.Excavator

© 2022 - 2024 — McMap. All rights reserved.