C# – Check if a property is an enum with reflection

When you’re using reflection to look at a type’s properties, you can use PropertyInfo.PropertyType.IsEnum to check if the property is an enum. This is helpful when you want to be able to safely call an Enum API method (such as Enum.Parse()) on the reflected type, thus preventing an exception – ArgumentException: Type provided must be an Enum.

Here’s an example of checking if a property is an enum, and then using an Enum API method:

var prop = typeof(Coder).GetProperty("Language");

if (prop.PropertyType.IsEnum)
{
	foreach(var enumValue in Enum.GetValues(prop.PropertyType))
	{
		Console.WriteLine(enumValue);
	}
}
Code language: C# (cs)

Since the Coder.Language property is an enum, this outputs all possible values for the enum:

None
CSharp
Java
PythonCode language: plaintext (plaintext)

I’ll show a few examples of using reflection with enum properties.

Check if a property is a specific enum type

If you want to check if the property is a specific enum type, you can do a type comparison instead of checking PropertyType.IsEnum:

var prop = typeof(Coder).GetProperty("Language");

if (prop.PropertyType == typeof(ProgrammingLanguage))
{
	//do stuff
}
Code language: C# (cs)

Set an enum property to a string

You can’t directly set an enum property to a string. When you try this, you get an exception like – ArgumentException: Object of type ‘System.String’ cannot be converted to type ProgrammingLanguage.

Instead, you have to parse the string with Enum.Parse() (or TryParse()). Here’s an example:

var newLanguage = "CSharp"; //from user input, JSON, etc...

var coder = new Coder();

var prop = coder.GetType().GetProperty("Language");

if (prop.PropertyType.IsEnum && Enum.IsDefined(prop.PropertyType, newLanguage))
{
	prop.SetValue(coder, Enum.Parse(prop.PropertyType, newLanguage));
}

Console.WriteLine(coder.Language);
Code language: C# (cs)

This outputs the value of the Coder.Language property:

CSharp

To reiterate, when using Enum API methods with reflection, it’s a good idea to use PropertyType.IsEnum to verify you’re dealing with an enum. Furthermore, use Enum.IsDefined() with Enum.Parse() to prevent parsing an invalid enum value and getting an exception (or use TryParse()).

Set an enum property to a numeric value

You can set an enum property to a numeric value with reflection without having to parse it. You can actually set it to any numeric value, even if it’s not defined on the enum, but I suggest using Enum.IsDefined() to guard against this. Here’s an example:

var newLanguage = 2; //from user input, JSON, etc...

var coder = new Coder();

var prop = coder.GetType().GetProperty("Language");

if (prop.PropertyType.IsEnum && Enum.IsDefined(prop.PropertyType, newLanguage))
{
	prop.SetValue(coder, newLanguage);
}

Console.WriteLine(coder.Language);
Code language: C# (cs)

This sets coder.Language to 2, which maps to ProgrammingLanguage.Java. Hence, this outputs:

JavaCode language: plaintext (plaintext)

Leave a Comment