I’m writing a method that do a cast and I need receive an type as parameter, for example:
object foo(?? type, object input) {
if(type is x ) {
Output output = new Output();
x xValue = (x) input;
foreach(var val xValue) {
//do..
}
return output;
}
if(type is y) {
Output2 output = new Output2();
y yValue = (y) input;
foreach(var val yValue) {
//do..
}
return output;
} else {
//invalid type
}
}
Use a type parameter:
Also, your method is a little weird, there seems to be a lot of commonality in your branches, and testing for types is not normally a good approach. Make an effort to unify them or split the method in type-specific overloads:
And, try to use a more specific return type, as shown above.