我有這樣的事情如何調用類型T的傳遞T作爲字符串或「T」的方法
public void FunctionName<T>(){
}
,並呼籲這一點,你需要的是這樣的
sub main(){
FunctionName<Integer>();
}
我的問題是我可以通過整數作爲「整數」?
sub main(){
FunctionName<"Integer">();
}
或有任何其他方式向
確定這裏是我的實際代碼
private static T Convert<T>(DataRow row) where T : new()
{
T model = new T();
Type modelType = model.GetType();
FieldInfo[] fields = null;
FieldInfo field = default(FieldInfo);
Type fieldType = null;
string fieldName = null;
fields = modelType.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);
foreach (field in fields) {
fieldType = field.FieldType;
fieldName = field.Name.Replace("_", string.Empty);
Interaction.CallByName(model, fieldName, Microsoft.VisualBasic.CallType.Set, GetValue(row, fieldName, fieldType.Name));
}
return model;
}
static void main(){
Student s;
s = Convert<Student>(DatabaseRow);
}
這裏的問題是我只能得到學生爲 字符串(「學生」 )從某個地方,將使用此代碼 任何其他解決方案,以得到這個權利?
WTF是與'子'的東西? – leppie 2012-08-05 16:11:37
@泰米爾:這個問題看起來很清楚。 – 2012-08-05 16:14:05
你確定你用C#編程嗎?也許你應該改變你的問題的標籤,包括實際的語言('sub'聽起來像VB) – Tipx 2012-08-05 16:19:37