[英]Can a get DbType from Dapper DynamicParameters?
I need iterate through Dapper DynamicParameters
.我需要遍历 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.现在,我需要参数 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!我不知道 Dapper 是否为此提供了更好的解决方案,但多亏了反思,没有什么不可能!
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>();
然后,这个调用很简单:
var dbTypes = GetParameterType<T>();
where T
is your object其中
T
是你的对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.