Cast shapes with interface

Asked

Viewed 53 times

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.

  • I want to cast the class. I will edit the question to try to be clearer.

  • @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.

  • 4

    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.

1 answer

5


You can leave it to the compiler to solve this cast for you. This can be easily accomplished with the use of a parameter restriction. just create the method this way:

public class Program
{
    public static void Main()
    {
        ClasseTeste1 teste1 = new ClasseTeste1();
        ClasseTeste2 teste2 = new ClasseTeste2();

        MeuMetodoDinamico(teste1);
        MeuMetodoDinamico(teste2);
    }

    // Aqui está a mágica do negócio, você exige que o parâmetro chamado 
    // seja um objeto que implementa a interface IMinhaInterface
    public static void MeuMetodoDinamico<T>(T parametro) where T : IMinhaInterface
    {
        parametro.MeuMetodo();
    }
}

public interface IMinhaInterface {
    void MeuMetodo();
}

public class ClasseTeste1 : IMinhaInterface {
    public void MeuMetodo() {
        Console.WriteLine("Classe Teste 1");        
    }
}

public class ClasseTeste2 : IMinhaInterface {
    public void MeuMetodo() {
        Console.WriteLine("Classe Teste 2");
    }
}

See working on . NET Fiddle.

  • Not generic also works :) https://dotnetfiddle.net/XCcmdJ

  • 1

    I don’t quite understand what problem the answer solves. Could you elaborate what would be the advantage of this that was answered in relation to simply do what @bigown put as an example in dotnetfiddle in the previous comment, just so we understand better?.

  • 1

    What is the purpose of using Generics in this answer?

  • Without Generics also works, in fact the question is a little confused, when I answered, I was leaving and did not notice the purpose of the question. There is probably some structure problem in the code, because if you need to cast the object, it makes no sense to use the interface.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.