全部,我有一个返回List的方法.此方法用于根据名称返回SQL StoredProcedures,Views和Functions的参数.我想要做的是创建一个对象列表并将此列表返回给调用者.方法如下
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;
}
Run Code Online (Sandbox Code Playgroud)
我显然无法实例化Tvia new并传递给构造函数,但应该清楚我正在尝试做什么.有没有办法用另外三种方法做我想做的事情?
注意.主要问题是我传递给T的参数数量可以是两个或三个.
谢谢你的时间.
编辑:struct我使用的是如下
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;
}
}
Run Code Online (Sandbox Code Playgroud)
使用Activator该类创建T并传递参数.
Type type = typeof(T);
var result = (T)Activator.CreateInstance(type, new object[] { yourParameters });
Run Code Online (Sandbox Code Playgroud)
在您的代码段中使用:
T t = Activator.CreateInstance(type, colInfo[NAME], colInfo[TYPE], Convert.ToInt32(colInfo[CHARMAXLEN]));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
580 次 |
| 最近记录: |