Reputation: 93
I would like to take provided enum
list and convert it to mine list. I am getting a list from a service Type and then I would like to convert it to mine FieldTyp
via DocumentFieldTypeConverter()
However, this dent work because enum
is not valid at this point dic.Add(i, Enum.GetName(rgl, FieldType));
I am trying this for the first time so apologies if this is complete nonsense. Prior to this I have used just switch control but I thought there could be something better
class Program {
static void Main(string[] args) {
var x = new Type().DocumentFieldTypeConverter();
foreach(var kvp in x) {
Console.WriteLine($ "{kvp.Key}: {kvp.Value}");
}
Console.ReadLine();
}
}
public static class EnumExtension {
public static Dictionary < int,
FieldType > DocumentFieldTypeConverter(this Type rglFieldType) {
var dic = new Dictionary < int,
FieldType > ();
var rgl = type.GetType();
foreach(int i in Enum.GetValues(rgl)) {
dic.Add(i, Enum.GetName(rgl, FieldType));
}
return dic;
}
}
public enum FieldType {
Document_Class_Code,
Document,
Document_Number,
DocumentType,
Passport_Number,
Invitation_Number,
}
public enum Type {
Document_Class_Code,
Document,
Document_Number,
DocumentType,
Passport_Number,
Invitation_Number,
}
Upvotes: 1
Views: 84
Reputation: 2540
Use below generic function to convert enums -
public static class EnumExtension
{
public static Dictionary<int, TDestinationType> EnumTypeConverter<TSourceType, TDestinationType>()
where TSourceType : struct, IConvertible
where TDestinationType : struct, IConvertible
{
var dictionary = new Dictionary<int, TDestinationType>();
var sourceEnum = typeof(TSourceType);
var sourceValues = Enum.GetValues(sourceEnum);
foreach (int enumVal in sourceValues)
{
var enumString = enumVal.ToString();
var destinationEnum = Enum.Parse(typeof(TDestinationType), enumString);
dictionary.Add(enumVal, (TDestinationType)destinationEnum);
}
return dictionary;
}
}
Call this from main as -
public static void Main(string[] args)
{
var enumVals = EnumExtension.EnumTypeConverter<Type, FieldType>();
foreach (var enumVal in enumVals)
{
Console.WriteLine($"{enumVal.Key}: {enumVal.Value}");
}
}
Upvotes: 1
Reputation: 2250
Type
is a keyword so I suggest using another name for the 2nd enum.
Here are a few methods, I suggest using the Enum.TryParse
to check for the conversion.
public enum FieldType
{
Document_Class_Code,
Document,
Document_Number,
DocumentType,
Passport_Number,
Invitation_Number,
}
public enum FieldTypeCopy
{
Document_Class_Code,
Document,
Document_Number,
DocumentType,
Passport_Number,
Invitation_Number,
}
FieldType fieldType = FieldType.DocumentType;
// Explicit conversion using the order
int i = (int)fieldType;
FieldTypeCopy fieldTypeCopy1 = (FieldTypeCopy)i;
Console.WriteLine("Copy 1: " + fieldTypeCopy1.ToString());
// Parse without check
FieldTypeCopy fieldTypeCopy2 = (FieldTypeCopy)Enum.Parse(typeof(FieldType), fieldType.ToString(), true);
Console.WriteLine("Copy 2: " + fieldTypeCopy2.ToString());
// Parse with check
if (Enum.TryParse(fieldType.ToString(), out FieldTypeCopy fieldTypeCopy3))
{
Console.WriteLine("Copy 3: " + fieldTypeCopy3.ToString());
}
else
{
Console.WriteLine("Unable to Parse value to enum");
}
Upvotes: 1