mardi 22 mai 2018

Calling methods with different parameters using a map instead of switch/if else

I am working a c# method which calls different methods based on some conditions and I am wondering if I can do this without switch or if else statements. Below is the code I have

if (msg== atype)
{
    _aHandler.HandleAType(msg, TopicType.A);
}
else if (msg== btype)
{
    _
    _btype.HandleBType(msg, TopicType.B);
}
else if (msg== ctype)
{
    _cHandler.HandleC(msg);
}
else if (msg== dtype)
{
    _dHandler.HandleDType(msg);
}

else
    _logger.Error($"No matching type found for {msg}");

Please note that I have different methods with different types of parameters in each condition.

Is there a better way I can do this without switch/if-else ?

Aucun commentaire:

Enregistrer un commentaire