2
I would like to instantiate a class from an Enum, for example:
Instead of doing:
var classeGenerica;
if (viewModel.tipoCarro == 1)
classeGenerica = new classeCarro();
else
classeGenerica = new classeOutros();
Something that went something like this:
TipoCarroEnum genericoEnum = (TipoCarroEnum)tipoCarro;
var generico = new genericoEnum();
I don’t understand. You want to use a
Enum
to instill a class? You cannot instantiate aEnum
.– CypherPotato
wanted to convert it into type or object for instantiation
– aa_sp
For what? What do you want to take from a so-called instance of
Enum
? An enumerator is nothing more than an abstract class where each value inside represents aint
.– CypherPotato
to avoid making an if for each type
– aa_sp
Makes a
switch
if you want to assign each value of theenum
for a specific class. Or if you want, you can use theEnum.Parse()
but he doesn’t fit his situation. It really doesn’t make sense to want to instantiate aenum
, besides being impossible, you will actually be instantiating an entire number, not a class.– CypherPotato
I understood what you want to do. In other languages it would be an easier job. Take a look at Factory Pattern implementations for C#, maybe give you some ideas.
– vinibrsl