❔ Can I generalise this method?

DDultus1/24/2023
public static IEnumerable<Enum> TransformIdToEnums(string value, params Type[] enums)
{
    string[] values = value.Split(',');
    for (int i = 0; i < enums.Length; i++)
    {
        var type = enums[i];
        yield return (Enum)Converter.GetEnumFromValue<type>(values[i]);
    }
}

Lets say I pass in "0,3,2" - I also want to pass in the Types of the Enusm I want to get them converted as. For example I split them into three different Enums, 0 being the category, 3 the subcategory and 2 a list item.
Any way to generalise this?
AAntonC1/24/2023
that enum method should have a non-generic overload
AAntonC1/24/2023
which takes the type as one of the arguments
AAntonC1/24/2023
if not, you can resolve the generic with reflection, but that's generally bad
TTheRanger1/24/2023
this worked for me
TTheRanger1/24/2023
    public static IEnumerable<Enum> TransformIdToEnums(string value, params Type[] enums)
    {
        string[] values = value.Split(',');
        for (int i = 0; i < enums.Length; i++)
        {
            var type = enums[i];
            var index = int.Parse(values[i]);
            yield return (Enum)Enum.GetValues(type).GetValue(index);
        }
    }
DDultus1/24/2023
Yeah, built it somewhat like that. Thanks!
DDultus1/24/2023
public static IEnumerable<Enum> TransformIdToEnums(string value, params Type[] enums)
{
    string[] values = value.Split(',');
    if (values.Length != enums.Length)
    {
        throw new MismatchedEnumLengthsException();
    }
    for (int i = 0; i < enums.Length; i++)
    {
        yield return (Enum)Enum.Parse(enums[i], value);
    }
}
AAccord1/25/2023
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.