Tutti, ho un metodo che restituisce una lista. Questo metodo viene utilizzato per restituire i parametri di SQL StoredProcedures, Views e Functions in base al nome. Quello che voglio fare è creare una lista di oggetti e restituire questa lista al chiamante. Il metodo è al di sottoC# 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;
}
ho chiaramente posso non un'istanza di T
via new
e passare al costruttore, ma dovrebbe essere chiaro ciò che sto cercando di fare. C'è un modo per fare ciò che voglio senza tre metodi aggiuntivi?
Nota. Il problema principale è che il numero di parametri che sto passando a T può essere due o tre.
Grazie per il vostro tempo.
Edit: I struct
s che uso sono i seguenti
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;
}
}
Quale * è * il tipo di 'T' probabile che sia qui? –
@Jon Skeet, in realtà è una 'struct'. Sto tentando di costruire una struttura ad albero riutilizzabile per mostrare database, tabelle ecc. Ma voglio anche essere in grado di usare di nuovo le informazioni ottenute da questa procedura - quindi uso un modello singleton con 'struct StoredProcedure',' struct Function' etc . Grazie. – MoonKnight
Suppongo che "InputParmaters" sia un refuso? – comecme