Receive by parameter a Varied object list

2

I have the following problem described in the comment:

public class Funcionario{
    public long Id {get; set;}
    public string Nome {get; set;}
    public DateTime DataContrato {get; set;}
}

public class Carro{
    public long Id {get; set;}
    public string Nome {get; set;}
    public bool IsUsado {get; set;}
}

// Aqui preciso receber uma lista que as vezes será de Funcionario e outras vezes de Carro;

public static MvcHtmlString(this HtmlHelper html, List<???> listaObj){
    //Aqui dentro eu preciso saber se a lista que estou recebendo no parametro é uma lista de Funcionario ou outros...
}
    
asked by anonymous 26.10.2016 / 21:26

2 answers

5

You should use a generic method to check the type inside. But ideally, make a specialized method for this case ( example ). I could do something like this:

using System.Collections.Generic;
using static System.Console;
using System;

public class Program {
    public static void Main() {
        MvcHtmlString(new List<Funcionario> { new Funcionario() });
        MvcHtmlString(new List<Carro> { new Carro() });
    }
    public static void MvcHtmlString<T>(List<T> listaObj) {
        if(typeof(T) == typeof(Funcionario)) {
            WriteLine("É lista de Funcionários");
        } else {
            WriteLine("Não é lista de Funcionários");
        }
    }
}

public class Funcionario{
    public long Id {get; set;}
    public string Nome {get; set;}
    public DateTime DataContrato {get; set;}
}

public class Carro{
    public long Id {get; set;}
    public string Nome {get; set;}
    public bool IsUsado {get; set;}
}

See running on dotNetFiddle and on CodingGround .

The secret there, besides using a generic type, is to check which type is being used. There are two ways to check the type:

  • One is at runtime and is applied to existing objects. The GetType() method is available on all objects. It returns the object of type Type that can be used to do a series of things, including comparisons;
  • another is used at compile time and can only be applied to types directly. The typeof
26.10.2016 / 22:14
4

What you really need is to learn to use the typeof operator and the GetType() method. With them, you can check the type of a particular variable. Note that every C # class has a GetType() method. Basically, you use typeof(T) , where T is any type. E inst.GetType() , where inst is a variable of any type.

There is also the is operator, you can see more details about the differences here.

There are several ways you can do this. The simplest is to ask for a object and check what type within the method.

Can also be done with Generics or inheritance, depending on the architecture of the application, realize that I'm not saying that you should use inheritance just because you need a method like this, I'm saying it's possible to do it if it already exists a "base" class among the types the method should receive, inheritance is possibly the best output.

I'd need more details to give you an answer that fits your problem better, yet it's impossible to say for sure, because only you know how much you want to "plaster" it.

Below is an example using object and checking the type within the method.

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        var lista1 = new List<Carro> { new Carro() };
        var lista2 = new List<Funcionario> { new Funcionario() };

        MvcHtmlString(lista1);
        MvcHtmlString(lista2);
    }


    public static void MvcHtmlString(object lista)
    {
        if(lista.GetType() == typeof(List<Carro>))
        {
            Console.WriteLine("Lista de carros");
        } 
        else if(lista.GetType() == typeof(List<Funcionario>))
        {
            Console.WriteLine("Lista de funcionários");
        }
    }
}
    
26.10.2016 / 21:34