Here's what I've used to create a generic method for calling stored procedures with generic output parameters.
public void CallProcedure<I, O>(
string storedProcedure,
I inputParameters,
O outputParameters,
string connectionId = "Default")
{
using IDbConnection connection =
new SqlConnection(_config.GetConnectionString(connectionId));
var dynamicParameters = new DynamicParameters();
if (inputParameters != null)
dynamicParameters.AddDynamicParams(inputParameters);
if (outputParameters != null)
foreach (PropertyInfo prop in outputParameters.GetType().GetProperties())
dynamicParameters.Add(prop.Name,
prop.GetValue(outputParameters),
GetDbType(prop.PropertyType),
ParameterDirection.Output);
connection.ExecuteAsync(storedProcedure, dynamicParameters,
commandType: CommandType.StoredProcedure);
if (outputParameters != null)
foreach (PropertyInfo prop in outputParameters.GetType().GetProperties())
{
var method = typeof(DynamicParameters)
.GetMethod("Get")
.MakeGenericMethod(new Type[] { prop.PropertyType });
var outputParamValue = method.Invoke(dynamicParameters, new object[] { prop.Name });
prop.SetValue(outputParameters, outputParamValue);
}
}
What you are looking for is the last few lines, below the second if (outputParameters != null)
I do not know how fast it is, as this code is a part of a sketch project.
I also asked this question 6 years later, so you can check if anyone answered it here: Get all stored procedure output parameters using generic type in dapper
All in all this is a good headsup for dapper team to create a simple method we can call to get all output parameters of a stored procedure, instead of us getting into reflection.