cezarypiatek / MappingGenerator

:arrows_counterclockwise: "AutoMapper" like, Roslyn based, code fix provider that allows to generate mapping code in design time.
https://marketplace.visualstudio.com/items?itemName=54748ff9-45fc-43c2-8ec5-cf7912bc3b84.mappinggenerator
MIT License
1.03k stars 120 forks source link

[Feature Request] - Enum <-> String #68

Closed bseng closed 5 years ago

bseng commented 5 years ago

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.

cezarypiatek commented 5 years ago

Hi

What is enum description function ? Could you provide sample mapping code that you expecting in described situation?

bseng commented 5 years ago

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);

ref: https://docs.microsoft.com/en-us/dotnet/api/system.enum.parse?view=netframework-4.7.2#System_Enum_Parse_System_Type_System_String_

cezarypiatek commented 5 years ago

Ok, and how to handle Enum -> String conversion? Just simply invoking ToString() method?

bseng commented 5 years ago

Yes.

Ref; https://docs.microsoft.com/en-us/dotnet/api/system.enum.tostring?view=netframework-4.7.2#System_Enum_ToString_System_String_

cezarypiatek commented 5 years ago

ok, I will try to make this kind of adjustment to the mapping rules

bseng commented 5 years ago

Thanks a lot for your work.

cezarypiatek commented 5 years ago

@bseng here's a version with this feature, would you mind to test it? Any feedback is welcome.

https://ci.appveyor.com/api/buildjobs/xognjdrwd41ohp85/artifacts/MappingGenerator%2FMappingGenerator%2FMappingGenerator.Vsix%2Fbin%2FRelease%2FMappingGenerator.vsix

cezarypiatek commented 5 years ago

Hi @bseng, did you have an occasion to test this new feature?

bseng commented 5 years ago

I haven't got a chance yet.

bseng commented 5 years ago

It works great thanks!

cezarypiatek commented 5 years ago

Thanks for your time and the feedback, I really appreciate it.