Wszystko, mam metodę, która zwraca listę. Ta metoda służy do zwracania parametrów SQL StoredProcedures, widoków i funkcji w zależności od nazwy. Co chcę zrobić, to utworzyć listę obiektów i zwrócić tę listę do dzwoniącego. Metoda ta jest poniżejC# Generics Natychmiastowa
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 wyraźnie może nie wystąpienia T
poprzez new
i przekazać do konstruktora, ale powinno być jasne, co ja próbuje zrobić. Czy istnieje sposób robienia tego, co chcę, przy pomocy trzech dodatkowych metod?
Uwaga. Głównym problemem jest to, że liczba parametrów, które przechodzę do T, może wynosić trzy: OR.
Dzięki za poświęcony czas.
EDIT: struct
s używam są następujące
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;
}
}
Co to jest * typ "T", który może być tutaj? –
@Jon Skeet, W rzeczywistości jest to 'struct'. Próbuję zbudować strukturę drzewa wielokrotnego użytku, aby wyświetlić bazy danych, tabele itp., Ale chcę również móc ponownie korzystać z informacji uzyskanych z tej procedury - dlatego używam wzorca singleton z 'struct StoredProcedure',' struct Function' etc . Dzięki. – MoonKnight
Przypuszczam, że '" InputParmaters "' jest literówka? – comecme