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?
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);
}
}
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.