Me preguntaba si lo siguiente es posible. Cree una clase que acepte un tipo anónimo (cadena, int, decimal, customObject, etc.) y luego tenga métodos sobrecargados que realicen diferentes operaciones basadas en el tipo. EjemploMétodo de sobrecarga con los tipos C#
class TestClass<T>
{
public void GetName<string>()
{
//do work knowing that the type is a string
}
public string GetName<int>()
{
//do work knowing that the type is an int
}
public string GetName<int>(int addNumber)
{
//do work knowing that the type is an int (overloaded)
}
public string GetName<DateTime>()
{
//do work knowing that the type is a DateTime
}
public string GetName<customObject>()
{
//do work knowing that the type is a customObject type
}
}
Así que ahora podría llamar al método GetName, y porque ya pasé en el tipo cuando inicializa el objeto, se encuentra el método correcto y ejecutado.
TestClass foo = new TestClass<int>();
//executes the second method because that's the only one with a "int" type
foo.GetName();
¿Es esto posible o solo estoy soñando?
+1 Generics! = Plantillas – user7116