this post was submitted on 11 Jul 2024
5 points (100.0% liked)
C Sharp
1537 readers
1 users here now
A community about the C# programming language
Getting started
Useful resources
- C# documentation
- C# Language Reference
- C# Programming Guide
- C# Coding Conventions
- .NET Framework Reference Source Code
IDEs and code editors
- Visual Studio (Windows/Mac)
- Rider (Windows/Mac/Linux)
- Visual Studio Code (Windows/Mac/Linux)
Tools
Rules
- Rule 1: Follow Lemmy rules
- Rule 2: Be excellent to each other, no hostility towards users for any reason
- Rule 3: No spam of tools/companies/advertisements
Related communities
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
I don't believe the first code sample is a valid C# code. Switch-case statements expect constants for case conditions and runtime types are not constants, obviously. What you can do is something like this:
This will work, but the problem here is that inheritance will fuck you up. All the types we check in this example are sealed and we don't really have to worry about it in this case, but generally this code is... just bad.
And yes, the correct way of implementing something like this is by using generic types. Starting from C# 7.0 we have this cool structure:
You'll have to check for null separately, as you can't use
Nullable<>
there, but overall, it looks much cleaner and handles inheritance properly for you.This is called pattern matching, you can read more about it here: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns
The switch case was based on an enum but it is what I want to get rid of. In the end I ended up doing what you wrote there, expect instead of casting I'm just writing
It just feels like I'm doing something wrong if i have to manually handle every case in a switch (or if else) statement and I was wondering how could I write, for example, a method that would do the conversion from Type.Long to System.Int64 for me, and then I just pass that type into the generic method instead of having to manually translate it into a type every time it is used.
However, if I have to use reflection maybe hardcoding it manually every time is actually faster and easier to debug so maybe i'm just overthinking it.
That c# 7 structure looks interesting but not sure it solves my issue, I need to get to the part where i have the generic type T in the first place. I dont know how to get a "T" out of a custom field indicating type, so to speak.
edit: as for the invalid code, i just wrote it quickly as example but you are right. Pretend it says switch (field.SomeType) instead of it being a method
You could still do it that way with a switch. Only the case part needs to be constant...
` switch (field.GetType().ToString()) {
case "Int": Method((int)x)...
case "NullInt": Method((int?)x)...
case "Long": Method((long)x)... `
Been a while since I last did this though - you may need to do string caseType=field.GetType().ToString() first, then do switch(caseType). I think from memory you can do it the other way though.
P.S. I clicked on "code" (which just starts/ends with an apostrophe), but it doesn't want to display as code - I don't know why