C
C#2y ago
Hugh

✅ Switching on a type object

Is there a way to do a switch on a type object? I've got a function that takes a type and returns an enum value for specific types. Currently I've had to do it like this:
private static MyValueType GetValueTypeFromType(Type value)
{
if(value == typeof(string))
{
return MyValueType.String;
}

if(value == typeof(bool))
{
return MyValueType.Bool;
}

if(value == typeof(int))
{
return MyValueType.Int;
}

return MyValueType.Invalid;
}
private static MyValueType GetValueTypeFromType(Type value)
{
if(value == typeof(string))
{
return MyValueType.String;
}

if(value == typeof(bool))
{
return MyValueType.Bool;
}

if(value == typeof(int))
{
return MyValueType.Int;
}

return MyValueType.Invalid;
}
However, I'd like to be able to make this code more concise. I could do it with a Dictionary, but if there's a better way of doing this, I'd love to hear it:
Dictionary<Type, MyValueType> valueTypes = new()
{
[typeof(string)] = MyValueType.String,
[typeof(bool)] = MyValueType.Bool,
[typeof(int)] = MyValueType.Int,
};
Dictionary<Type, MyValueType> valueTypes = new()
{
[typeof(string)] = MyValueType.String,
[typeof(bool)] = MyValueType.Bool,
[typeof(int)] = MyValueType.Int,
};
5 Replies
HimmDawg
HimmDawg2y ago
You can only use pattern matching in a switch statement, if you have an object, not the type object itself. So you have to rely on a dictionary, or maybe even switch on the name of the type.
Hugh
Hugh2y ago
Okay - thanks - that's good to know I'm happy with the dictionary approach. I made the actual dictionary a private static readonly field, so it isn't re-generating each time
HimmDawg
HimmDawg2y ago
I think there is some syntax like
switch (type)
{
case Type t1 when t1 == typeof(int): ...
}
switch (type)
{
case Type t1 when t1 == typeof(int): ...
}
but it's not really better than if/else 😄
ero
ero2y ago
in fact i'm pretty sure it's a lot worse the compiler can optimize if (typeof(T) == typeof(int)) ...
Hugh
Hugh2y ago
Thanks - I think I'll stick with the ifs then
Want results from more Discord servers?
Add your server
More Posts
❔ Bullet World.Step causes 0xC000001D Illegal Instruction (C BulletSharp)So i am using BulletSharp in my project. This is the code i use to step it: if (settingScene) retur❔ 21,000 Images, Best way to implement a lookup method?I have 21,000 + images that have to be available for lookup. The images are formatted [item_id].png✅ How Do I Save a New Fully Defined Relationship in EF Core?Ok, so if I have a token which looks like this ```cs /// <summary> /// Represents a token for a deviJavascript/JQuery 3.6.0 help [Self-Resolved]I have to be missing something silly. The idea is there are three levels of cascading dropdowns: Ca❔ ✅ AES - Padding is invalid and cannot be removedI am attempting to quickly encrypt a set of files using AES, but I'm getting the above error when de❔ Is there any way to send a notification if the Windows is closed?Hi, I'm using System.Windows to open a Window and I know I can send notifications with NotifyIcon. I✅ Foreach vs Parallel.ForEach with Async mehtodsI recently changed my code who had foreach to Parallel.Foreach. This foreach has multiples asynchroncsproj not found in RiderHi, I just started coding in C#, following an tutorial from CodeWithPraveen, I have created an main ❔ how to use Docker for 2 apps in a project```html MyProject > WebApp > WebApi MyProject.sln ``` -They both require .NET 6.0.404 an❔ Optimize Compiler Code GenerationI am writing a compiler and I am almost done with all the base syntax/functionality, and am up to op