C
C#

help

❔ 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]);
}
}
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?
AAnton1/24/2023
that enum method should have a non-generic overload which takes the type as one of the arguments if not, you can resolve the generic with reflection, but that's generally bad
TTheRanger1/24/2023
this worked for me
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);
}
}
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!
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);
}
}
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.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
❔ c# helphow can i make ipv6 altering application using c#❔ Cross-platform keyboard and mouse automation library wantedAnyone knows a .NET library that is on par with pyautogui? Specifically, waiting for a key to be preHow to change the icon for Android in a Maui/Blazor application?Net 7.0, latest visual studio preview. I have a simple (beginner) one page Maui/Blazor app that is ❔ EF Core Code-first relationshipsHi there, I am trying to create a coffee vending machine application. But, I can't quite work the re❔ Hello, postman tips on hitting API with external login page? I need to get the 'code' response❔ Hello, I would like to make a console with the command: Script (some script)if (TextBox.Text == "script" { run script }❔ help me plsmain. cs(7,0): error CS1525: Unexpected symbol 'if'❔ Reusing strings across classesThis is file 1 https://cdn.discordapp.com/attachments/987310138195054592/1067462232860004433/image0❔ UWP TreeView is not stretching to fill spaceTitle pretty much explains it all: I have a TreeView that's not stretching and stays 0px x 0px if t❔ Is there an easy way to serialize a Dictionary<string, string> to XML nodes?I'm writing a program that has to produce the following XML: ```xml <Attributes> <Color>red</Col✅ Could not load file or assembly 'StackExchange.RedisWhat causes these errors? How do I fix it? The project builds find on my windows machine. But runnin❔ Async function seems to continue running after returning.Hi there I'm doing a post request to a REST API. I can see that the callback is running in the conso❔ best way to include and works with imagesI'm working on a project, and I'll have to work with several images. Actually the project is working✅ how does compiler connect several c# codes into 1?I've noticed that classes that i created in other file are accessible in the main class (all in 1 pr❔ DMZ Server SoftwareI am working on a project to create a DMZ Server that will pass information to a 3rd party from the ✅ How to correctly install dependencies with nuget and rider ?Hi! project complains about missing dependencies, I installed dependencies but it still complains ,❔ How can I avoid supplying redundant generic type arguments in this case.```interface IDataStructure<ItemType, LocatorType> {...} Class MyStringArray: IDataStructure<string✅ Hello, what is best practice for where to store an enum type?What is best practice for where to store an enum type(Which class library)? I have clean architectur