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");
}
}
}