Basically, it should not be avoided. You have to know how to use and when to use. This story of being "good practice" or "bad practice" is mostly an artifice used to impose rules without having to base what you are talking about.
Why does dynamic
be considered a practice to avoid?
Only those who have told you this can answer this question. But it is likely that this is because, using dynamic
, everything is resolved at runtime and not compiled, so it is much easier to do some bullshit and end up crashing the application.
Changing in kids, you simply "throw away" one of the great advantages of static languages is to know beforehand all (possible) existing members of an object.
A small example:
dynamic pessoa = new { Nome = "jbueno" };
var n = pessoa.nome;
This code compiles normally, but it bursts a run-time error because the nome
property does not exist in pessoa
.
Is there anything that can replace use with the same effect?
If by "effect" you mean dynamicity: the answer is no. And if this is really necessary I would even tell you that you are probably using the wrong language.
For other cases it is possible to give you some tips knowing the real need. Citing all possibilities is impractical.
What case where the use of dynamic
is required?
Depends a little. It is necessary whenever you can not know the structure of the object beforehand.
A real example would be to make a request to a webservice whose return might have two completely different structures.
Other cases are likely to be most useful. I find it difficult that the creation of this resource was to solve cases like this example.
{ "sucesso": "true" };
{ "erro": "Algo deu errado" };