我可以 return ValueTuple 中的通用类型吗?
Can I return a generic type inside of a ValueTuple?
下面是一些示例代码。我收到错误 "Cannot implicitly convert type 'string' to '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);
}
使用您的示例,我提出了以下内容作为如何执行您想要执行的操作的示例。这使用一个通用实现方法来重用您可能返回的各种类型,一个非通用方法来公开返回这些类型。
public ValueTuple<bool, List<KeyRow>> RunSelectAllKeyRowCommand(IDbCommand command, List<IDataParameter> parameterCollection) =>
RunSelectAllCommandImpl<KeyRow>(command,
parameterCollection,
reader => new KeyRow(reader["keycode"].ToString(), reader["apikey"].ToString(), reader["ipaddress"].ToString(), DateTime.Parse(reader["date"].ToString()), reader["hwid"].ToString() ?? ""));
private ValueTuple<bool, List<T>> RunSelectAllCommandImpl<T>(IDbCommand command, List<IDataParameter> parameterCollection, Func<IDataReader, T> mapper) where T : class
{
using (IDbConnection conn = GetDataConnection())
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = command.CommandText;
foreach(var parameter in parameterCollection) // if parameters for query are specified, add them here.
cmd.Parameters.Add(parameter);
using (var reader = cmd.ExecuteReader())
{
List<T> data = new List<T>();
while (reader.Read())
data.Add(mapper(reader));
return (true, data);
}
}
return (false, null);
}
下面是一些示例代码。我收到错误 "Cannot implicitly convert type 'string' to '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);
}
使用您的示例,我提出了以下内容作为如何执行您想要执行的操作的示例。这使用一个通用实现方法来重用您可能返回的各种类型,一个非通用方法来公开返回这些类型。
public ValueTuple<bool, List<KeyRow>> RunSelectAllKeyRowCommand(IDbCommand command, List<IDataParameter> parameterCollection) =>
RunSelectAllCommandImpl<KeyRow>(command,
parameterCollection,
reader => new KeyRow(reader["keycode"].ToString(), reader["apikey"].ToString(), reader["ipaddress"].ToString(), DateTime.Parse(reader["date"].ToString()), reader["hwid"].ToString() ?? ""));
private ValueTuple<bool, List<T>> RunSelectAllCommandImpl<T>(IDbCommand command, List<IDataParameter> parameterCollection, Func<IDataReader, T> mapper) where T : class
{
using (IDbConnection conn = GetDataConnection())
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = command.CommandText;
foreach(var parameter in parameterCollection) // if parameters for query are specified, add them here.
cmd.Parameters.Add(parameter);
using (var reader = cmd.ExecuteReader())
{
List<T> data = new List<T>();
while (reader.Read())
data.Add(mapper(reader));
return (true, data);
}
}
return (false, null);
}