1
wanted to know what are the possibilities (but which one is more suitable) to do a cast with interface.
public class ClasseTeste1 : IMinhaInterface { ... }
public class ClasseTeste2 : IMinhaInterface { ... }
See in the example that I am using it in the parameter passage in the method.
public void MeuMetodo(IMinhaInterface parametro)
{
var objetodaClasse1 = (ClasseTeste1)parametro;
var objetodaClasse2 = (ClasseTeste2)parametro;
//Mas qual classe seria a que veio pelo parâmetro?
...
}
In this case, instead of putting the class type, I would like to get the type by object. via some type of gettype for example.
See how I imagine it would be
public void MeuMetodo(IMinhaInterface parametro)
{
var objetodaClasse = (parametro.gettype())parametro;
...
}
However, I want to leave the definition of the class according to the parameter. Then I would need to have some Gettype to know which class will be instantiated.
What they tell me most in this scenario?
I do not understand what you want. It may not be possible or it is not the right one. Try to show with code what you would do.
– Maniero
I want to cast the class. I will edit the question to try to be clearer.
– Bruno Heringer
@Brunoheringer, take a look if the answer suits you. Just one detail, within the method, you will only use what is specific to the interface, correct? Because if you are going to use methods specific to each implementation, then the approach should be different.
– Julio Borges
It’s still unclear why you need this cast, something tells me you don’t need it. Put something else in the code that indicates why you need the cast.
– Maniero