Todo, tengo un método que devuelve una lista. Este método se usa para devolver los parámetros de SQL StoredProcedures, Views y Functions dependiendo del nombre. Lo que quiero hacer es crear una lista de objetos y devolver esta lista a la persona que llama. El método está por debajoC# Generics Instantiation
private List<T> GetInputParameters<T>(string spFunViewName)
{
string strSql = String.Format(
"SELECT PARAMETER_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.PARAMETERS " +
"WHERE SPECIFIC_NAME = '{0}' AND PARAMETER_MODE = 'IN';",
spFunViewName);
List<string[]> paramInfoList = new List<string[]>();
DataTable paramDt = Utilities.DTFromDB(conn, "InputParmaters", strSql);
if (paramDt != null)
{
Converter<DataRow, string[]> rowConverter =
new Converter<DataRow, string[]>(Utilities.RowColConvert);
paramInfoList = Utilities.ConvertRowsToList<string[]>(paramDt, rowConverter);
}
else
return null;
// Build the input parameter list.
List<T> paramList = new List<T>();
foreach (string[] paramInfo in paramInfoList)
{
T t = new T(paramInfo[NAME], paramInfo[TYPE], Convert.ToInt32(paramInfo[CHARMAXLEN]));
columnList.Add(column);
}
return columnList;
}
I claramente no puedo crear una instancia T
través new
y pasar al constructor, pero debe quedar claro lo que estoy tratando de hacer. ¿Hay alguna manera de hacer lo que quiero sin tres métodos adicionales?
Nota. El problema principal es que la cantidad de parámetros que paso a T puede ser dos O tres.
Gracias por su tiempo.
Editar: Los struct
s que uso son las siguientes
public struct Database
{
public string name { get; set; }
public string filename { get; set; }
public List<Table> tables { get; set; }
public List<StoredProcedure> sps { get; set; }
public List<Function> funcs { get; set; }
public List<View> views { get; set; }
public Database(string name, string filename)
{
this.name = name;
this.filename = filename;
}
}
protected internal struct StoredProcedure
{
public string name { get; set; }
public List<string[]> parameters { get; set; }
public StoredProcedure(string name, List<string[]> parameters)
{
this.name = name;
this.parameters = parameters;
}
}
protected internal struct Function
{
public string name { get; set; }
public string output { get; set; }
public List<string[]> parameters { get; set; }
public Function(string name, string output, List<string[]> parameters)
{
this.name = name;
this.output = output;
this.parameters = parameters;
}
}
protected internal struct View
{
public string name {get; set;}
public List<string[]> parameters { get; set; }
public View(string name, List<string[]> parameters)
{
this.name = name;
this.parameters = parameters;
}
}
¿Cuál * es * el tipo de 'T' probable que esté aquí? –
@Jon Skeet, en realidad es una 'struct'. Estoy intentando construir una estructura de árbol reutilizable para mostrar bases de datos, tablas, etc., pero también quiero poder utilizar la información obtenida de este procedimiento nuevamente, así que utilizo un patrón singleton con 'struct StoredProcedure',' struct Function', etc. . Gracias. – MoonKnight
Supongo que '" InputParmaters "' ¿es un error tipográfico? – comecme