Reputation: 29
I need iterate through Dapper DynamicParameters
. So, I check this answer to get value of parameter.
foreach (var paramName in parameters.ParameterNames)
{
var value = ((SqlMapper.IParameterLookup)parameters)[paramName];
}
Now, I need parameter DbType. Is it possible to get this information?
Upvotes: 0
Views: 1660
Reputation: 2186
You can do this! Please check the code below:
private List<DbType> GetParameterType<T>()
{
var type = typeof(T);
var properties = type.GetProperties().Select(property => property.PropertyType.Name).ToList();
var dbTypes = new List<DbType>();
foreach (var prop in properties)
{
var tryParse = Enum.TryParse<DbType>(prop, out var result);
if (tryParse)
dbTypes.Add(result);
}
return dbTypes;
}
And then, the call of this is simple: var dbTypes = GetParameterType<T>();
where T
is your object
Upvotes: 0
Reputation: 6566
I dont know if Dapper provides a better solution for this or not, but thanks to reflection, there is nothing impossible!
var t = parameters.GetType().GetField("parameters", BindingFlags.NonPublic | BindingFlags.Instance);
if (t != null)
{
foreach (DictionaryEntry dictionaryEntry in (IDictionary)t.GetValue(parameters))
{
var dbType = (DbType)dictionaryEntry.Value?.GetType().GetProperty("DbType")?.GetValue(dictionaryEntry.Value);
}
}
Upvotes: 3