Closed bseng closed 5 years ago
Hi
What is enum description function
? Could you provide sample mapping code that you expecting in described situation?
for example.
using System;
[Flags] enum Colors { None=0, Red = 1, Green = 2, Blue = 4 };
public class Example
{
public static void Main()
{
string[] colorStrings = { "0", "2", "8", "blue", "Blue", "Yellow", "Red, Green" };
foreach (string colorString in colorStrings)
{
try {
Colors colorValue = (Colors) Enum.Parse(typeof(Colors), colorString, true);
if (Enum.IsDefined(typeof(Colors), colorValue) | colorValue.ToString().Contains(","))
Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString());
else
Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString);
}
catch (ArgumentException) {
Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString);
}
}
}
}
// The example displays the following output:
// Converted '0' to None.
// Converted '2' to Green.
// 8 is not an underlying value of the Colors enumeration.
// Converted 'blue' to Blue.
// Converted 'Blue' to Blue.
// Yellow is not a member of the Colors enumeration.
// Converted 'Red, Green' to Red, Green.
So for string to enum if we can generate the following
Colors colorValue = (Colors) Enum.Parse(typeof(Colors), colorString, true);
Ok, and how to handle Enum -> String conversion? Just simply invoking ToString()
method?
ok, I will try to make this kind of adjustment to the mapping rules
Thanks a lot for your work.
@bseng here's a version with this feature, would you mind to test it? Any feedback is welcome.
Hi @bseng, did you have an occasion to test this new feature?
I haven't got a chance yet.
It works great thanks!
Thanks for your time and the feedback, I really appreciate it.
In the case of attributes with the same name and one is enum while the other one is string, use the enum tryparse and enum description function to do 2 way transform.