Method returning parent class to child class

3

I have the following classes:

public class Pessoa
{
    public int PessoaId { get; set; }
    public string Nome { get; set; }
    public DateTime DataNascimento { get; set; }
}

public class Cliente : Pessoa
{
    public int ClienteId { get; set; }
}
public class Funcionario : Pessoa
{
    public int FuncionarioId { get; set; }
}

I created the following method:

public Pessoa Teste(Pessoa pessoa)
{
    // Bloco lógico
    return pessoa;
}

What I would like to do now was to send a Funcionario or Cliente to the Teste method and get them back with their Pessoa properties changed.

The problem is that when using:

var func = new Funcionario();
func = Teste(func);

It does not work, because my func variable is of type Funcionario , and return in method Teste returns Pessoa . But since Funcionario inherits from Pessoa is there any way to do that?

    
asked by anonymous 27.06.2016 / 20:35

3 answers

5

In general Andrew's response works, after all if you're sure you've passed Funcionario to the method you know that casting an cast of the returned object to Funcionario will work. Without this certainty this would be a danger.

A more "modern" way of doing this is what Gabriel Katakura's response says. By doing a generic method you already ensure that the returned type will be the type you passed to the method, so you do not need make a cast and it is safer.

public T Teste<T>(T pessoa) where T : Pessoa {
    //faz o que deseja aqui
    return pessoa;
}

Call:

var func = new Funcionario();
func = Teste(func); //ele infere, se não fosse possível chamaria Teste<Funcionario>(func)

Note that in this case the compiler will generate a specialized method that will receive an object of any type and return an object of the same type. This is defined by the T parameter, which is a super variable of type. I said that it receives any object, but it is not quite like that. It has a restriction saying that T must be any type, as long as it is Pessoa , so derived types are worth, others are not. More details are in the linked question above.

When compiling this code it is as if it had written:

public Funcionario Teste(Funcionario pessoa) {
    //faz o que deseja aqui
    return pessoa;
}

But he did not have to write. This is the beauty of generosity. The compiler adapts your method to each type you use. It will be defined by the use of the method. This is called client site , or consumer site .

Obviously you can not do anything specific to an object Funcionario or Cliente , you can only do things available in Pessoa .

    
27.06.2016 / 21:06
3

Just use generics for this (short answer, I'm out of time, sorry):

public TPessoa Teste<TPessoa>(TPessoa pessoa)
    where TPessoa : Pessoa
{
    return pessoa;
}
    
27.06.2016 / 20:40
1

Have you tried a casting yet?

var func = new Funcionario();
func = (Funcionario)Teste(func);

Since Official is also a Person a simple casting should work.

    
27.06.2016 / 20:39