Guide on how to convert an Enum to another in .NET C#
While working on your software system you might find yourself dealing with different modules and some of these modules could be out of your full control.
This could happen for different reasons, may be due to some access restrictions, ownership, policy,…
In this kind of situations, you might face some challenges related to mapping different entities, classes, interfaces, enums,… to each other. It would not be a ship-stopper, but still you have to handle this in the best possible way.
In this article, we would explain more than one way to handle mapping different Enums to each other.
The Example
First, let’s agree on an example to use throughout the different article sections.
Let’s say that we have the following two Enums which we would like to handle converting between them both ways.
In the following article sections, we would discuss more than one method to handle the conversions.
On this method, we would assume that we don’t have any edit privileges on both Enums; ItemSize and StandardItemSize. This means that we can’t apply any changes on both of them.
In this case, what we can do is to create extension methods to handle the conversion between them.
Therefore, following this way of thinking, we would end up with something like this:
As you can see, we are just handling the conversions in a switch-case manner.
Then we can start using these extension methods as follows:
And the result would be as follows:
Nice, it is working as expected.
On this method, we would assume that we don’t have any edit privileges on ItemSize but we own StandardItemSize. This means that we can’t apply any changes on ItemSize but we can apply the required changes on StandardItemSize.
This is different than the case on Method 1Â as now we can make use of the fact that we can do some minor modifications to StandardItemSize.
So, instead of handling the conversion into a switch-case manner in a separate extension method, we can do some tagging on the enum itself and then use this tagging in the extension method.
This would be so useful as now the mapping between both enums would be consistent, readable and encapsulated into the StandardItemSize enum itself.
Therefore, following this way of thinking, we would define a new Attribute to be used for tagging the StandardItemSize enum members to map each of them to its related member in ItemSize.
It should be as follows:
And now we can use it as follows:
Then we can update the ToItemSize extension method as follows:
As you can notice here, we are writing code to depend on the RelatedItemSizeAttribute attribute on each StandardItemSize enum member to finally get the corresponding member of ItemSize enum.
Also, now we can update the ToStandardItemSize extension method as follows:
As you can notice here, we are writing code to depend on the RelatedItemSizeAttribute attribute on each StandardItemSize enum member to finally return the StandardItemSize member which is tagged with a RelatedItemSizeAttribute attribute corresponding to the passed in ItemSize member.
Now you might be concerned about the performance of these extension methods and I can understand you. You really need to check your application performance and see if it suits your needs or not.
You also need to keep in mind that you can always get back to Method 1Â for the direct mapping between both enums if you are not satisfied by the performance on this method.
Now, as usual we can start using these extension methods as follows:
And the result would be as follows:
Nice, it is working as expected.
On this method, we would assume that we own both ItemSize and StandardItemSize. This means that we can apply any changes on both of them.
So, what we would actually do here is extending the solution we came up with on Method 2.
So, we would define RelatedStandardItemSizeAttribute as follows:
And update ItemSize as follows:
And update ToStandardItemSize as follows:
Now, as usual we can start using these extension methods as follows:
And the result would be as follows:
Nice, it is working as expected.
Final Thoughts
In this article, we discussed more than one way to handle mapping and conversion between two enums when we have and don’t have control on both of them.
As I said before, you might have your doubts about the performance and for sure you have the right to think so. That’s why my advice to you is to check the impact and evaluate according to your needs.
That’s it, hope you found reading this article as interesting as I found writing it.
Comments