Pasar solo un tipo como parámetro en C#
Hipotéticamente sería útil para mí hacer esto:
foo.GetColumnValues(dm.mainColumn, int)
foo.GetColumnValues(dm.mainColumn, string)
Donde el método getColumns llamará a un método diferente dentro dependiendo del tipo pasado.
Sí, podría hacerlo como una bandera booleana o similar, solo me preguntaba si había una manera de tal vez pasar esto, y luego preguntar:
Typeof(arg [1]) o similar...
También podría anular el método, usar genéricos, etc. Sé que hay diferentes maneras de hacer esto, solo tenía curiosidad si esto era posible.
6 answers
Hay dos enfoques comunes. Primero, puedes pasar System.Type
object GetColumnValue(string columnName, Type type)
{
// Here, you can check specific types, as needed:
if (type == typeof(int)) { // ...
Esto se llamaría así: int val = (int)GetColumnValue(columnName, typeof(int));
La otra opción sería usar genéricos:
T GetColumnValue<T>(string columnName)
{
// If you need the type, you can use typeof(T)...
Esto tiene la ventaja de evitar el boxeo y proporcionar algún tipo de seguridad, y se llamaría así: int val = GetColumnValue<int>(columnName);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-08 20:22:43
foo.GetColumnValues(dm.mainColumn, typeof(string))
Alternativamente, puede usar un método genérico:
public void GetColumnValues<T>(object mainColumn)
{
GetColumnValues(mainColumn, typeof(T));
}
Y luego podrías usarlo como:
foo.GetColumnValues<string>(dm.mainColumn);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-08 20:22:21
Puede pasar un tipo como argumento, pero para hacerlo debe usar typeof
:
foo.GetColumnValues(dm.mainColumn, typeof(int))
El método tendría que aceptar un parámetro con el tipo Type
.
Donde el método getColumns llamará a un método diferente dentro dependiendo del tipo pasado.
Si desea este comportamiento, no debe pasar el tipo como argumento, sino usar un parámetro de tipo.
foo.GetColumnValues<int>(dm.mainColumn)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-08 20:27:51
foo.GetColumnValues(dm.mainColumn, typeof(int));
foo.GetColumnValues(dm.mainColumn, typeof(string));
O usando genéricos:
foo.GetColumnValues<int>(dm.mainColumn);
foo.GetColumnValues<string>(dm.mainColumn);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-08 20:23:10
Puede hacer esto, simplemente envuélvalo en typeof()
foo.GetColumnValues(typeof(int))
public void GetColumnValues(Type type)
{
//logic
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-08 20:22:31
Se puede usar un argumento de tipo Type - iow, pass typeof(int). También puede usar genéricos para un enfoque (probablemente más eficiente).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-08 20:22:38