You're looking for the Type.IsEnum
property, which does exactly that.
How to tell if object is an enum?
-
25-06-2022 - |
سؤال
I'm not a C# wizard, but there has to be a simple solution for this.
I have an IValueConverter
for a DataTemplate
. The converter will be used in a control whose data is bound to an object of type enum
: public enum ContentTypes
. I want to write a generic converter that can take any enum
and then do something with it however when the converter below does it's thing the object parameter's type is ContentTypes
rather than Enum
.
The Convert
method:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Type valueType = value.GetType(); // <-- evaluates to `ContentTypes`. How can I just see if it's an enum, or Enum?
if (value.GetType() == typeof(Enum))
{
// Do something ....
}
return null;
}
So, is it possible to take an object of unknown type and determine if it is an enum
and cast it to such an enum, generically? I'm missing something basic.
المحلول
لا تنتمي إلى StackOverflow