Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My Enum is not recognized using reflection and PropertyInfo

I've a audit class that recovery everything by reflection. I need in my actual point know if an specific property is an Enum, but I'm getting a strange behavior:

During foreach iteration q.PropertyType.IsEnum return false. And using Quick watcher the property is really false, and the IsClass too. So this is basically nothing :)

Studying a little more about the problem I found that Nullable Enum returns false in IsEnum. How can I ignore this nullable and verify if the property is an enum or not?

like image 563
Custodio Avatar asked May 17 '11 19:05

Custodio


People also ask

Is nullable enum C#?

Enum types cannot be nullable.


1 Answers

IsEnum will return false when your property is of a nullable type. In this case, calling Nullable.GetUnderlyingType on q.PropertyType will return the type you want. Then you can check with IsEnum.

like image 81
Fernando Avatar answered Oct 23 '22 23:10

Fernando