gpt4 book ai didi

c# - 我可以在 ValueTuple 中返回泛型类型吗?

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:32 24 4
gpt4 key购买 nike

下面是一些示例代码。我收到错误消息“无法将类型‘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);
}

最佳答案

使用您的示例,我提出了以下内容作为如何执行您正在尝试执行的操作的示例。这使用一个通用实现方法来重用您可能返回的各种类型,一个非通用方法来公开返回这些类型。

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);
}

关于c# - 我可以在 ValueTuple 中返回泛型类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49949534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com