这是一些示例代码。我收到错误“无法将类型'string'隐式转换为'T'”
public ValueTuple<string, T> TestMethod<T>()
{
return ("test", "");
}
我该怎么做呢?这超出了 ValueTuple 的限制吗?
我正在使用的真实代码示例
public ValueTuple<bool, T> RunSelectAllCommand<T>(MySqlCommand command,
List<MySqlParameter> parameterCollection) where T : class
{
using (MySqlConnection conn = new MySqlConnection(connectionString))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = command.CommandText;
// if parameters for query are specified, add them here.
if (parameterCollection != null)
{
cmd.Parameters.AddRange(parameterCollection.ToArray());
}
using (var reader = cmd.ExecuteReader())
{
// We're selecting all keys from registration database
// this is probably a horrible design. oh, well...
if (typeof(T) == typeof(KeyRow))
{
List<KeyRow> data = new List<KeyRow>();
while (reader.Read())
{
data.Add(new KeyRow(reader["keycode"].ToString(),
reader["apikey"].ToString(),
reader["ipaddress"].ToString(),
DateTime.Parse(reader["date"].ToString()),
reader["hwid"].ToString() ?? ""));
}
return (true, data as T);
}
}
}
return (false, null);
}